home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / sml_nj / 93src.lha / src / runtime / mac / os_mac_eEdit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-09  |  85.2 KB  |  2,990 lines

  1. /* os_mac_eEdit.c 
  2.  * 8Apr92  e
  3.  */
  4.  
  5. #include <MacHeaders>
  6.  
  7. #include "os_mac_eEdit.h"
  8.  
  9. /* an editor for text files by e
  10.    questions/comments via Internet <e@Flavors.COM> */
  11. /* Copyright ⌐ e, 1992. All rights reserved.
  12.     Developed using THINK C 5.0.1 for use with Gambit Scheme.
  13.     This code may be freely distributed as long as this notice remains.
  14.    based upon CEditor.C...
  15.     Copyright ⌐ BRH Toolsmith, 1992. All rights reserved.
  16.     Developed using THINK C 4.0.2 and Symantec's OOP libraries.
  17.     Portions of this code courtesy Symantec, Inc.
  18.     This code may be freely distributed as long as this notice remains. If you wish
  19.     to distribute modifications to these classes, please make a copy of the class
  20.     and implement your changes there. That's the beauty of OOP!
  21. since:
  22. a) I was not using Symantec's OOP, and
  23. b) CEditor.C had (due to the dependence on the Symantec libraries) a serious limitation,
  24.  to wit a limit of 32,767 pixels of document height (about 3000 lines of text),
  25. eEdit.c is a recasting of CEditor.C in vanilla C with very significant enhancements and fixes.
  26. */
  27.  
  28. /* 10Dec92  e  -- to do:
  29. - better out-of-memory error handling
  30. */
  31.  
  32. /* for arrow keys which ~work like ThinkC editor instead of Apple standard
  33. #define THINK_ARROWS (1)
  34. */
  35. /* for RETURN which auto-tabs like ThinkC editor - not good with Gambit
  36. 16Dec92  e  -- dont turn this on; it doesn't work anymore!
  37. #define THINK_RETURN (1)
  38. */
  39. /* to allow chars > 127 in the text... */
  40. #define CHAR8_OK (1)
  41.  
  42. /* eEdit documentation...
  43.  
  44.     Limitations
  45.         text size is limited by...
  46.             memory space
  47.               = ~200 + text + (#lines + #style-runs + MIN_LINES + MIN_RUNS) * 4
  48.             32K lines max
  49.             32K style runs max
  50.           eEdit's been tested with files several hundred K in size
  51.           there is no pixel height limit (which constrains many editors to ~3000 lines)
  52.         exactly two styles per text
  53.  
  54.     eTeKey()
  55.         EXTENDED KEYBOARD:
  56.             F1 - F4 - Undo, Cut, Copy, Paste
  57.             F5 - F15 - function keys (not implemented)
  58.             KeyDel - delete forward, erase character after cursor position
  59.             [these next four do not move the insert point, just scroll]
  60.             KeyHome - scrolls text to the top of the screen
  61.             KeyEnd - scrolls text to the bottom of the screen
  62.             KeyPageUp - scrolls text up a page
  63.             KeyPageDown - scrolls text down a page
  64.  
  65.         ALL KEYBOARDS
  66.             LEFT_ARROW - move cursor to the left one character
  67.             RIGHT_ARROW - move cursor to the right one character
  68.             UP_ARROW - move cursor up one line
  69.             DOWN_ARROW - move cursor down one line
  70.             DELETE - erase character before cursor position
  71.             RETURN/ENTER - insert new line and move to left margin (insert a RETURN)
  72.             TAB - insert TAB character; displayed as N spaces
  73.             0x20 - 0xFF - range of valid text characters
  74.  
  75.       Arrow keys can be modified with shift, option, and command
  76.         shift - extend selection per Apple standard (optionally ThinkC mode)
  77.         option - left/right by word, may be used with shift key to extend by word
  78.         option - up/down by page, may be used with shift key to extend by page
  79.         option+shift up/down arrows make the start/end of the selection the active end
  80.         command - left/right: start/end of line, up/down: start/end of text
  81.                     may be used with shift key to extend selection
  82.  
  83.     *** NOTE: 
  84.     *** command-arrow keys will work ONLY IF the main event loop
  85.     *** lets command keys not associated with menu items pass thru to eEdit
  86.  
  87.     eTeClick()
  88.         Mouse clicks
  89.             single - position insert point
  90.             double - select word
  91.             triple - select line
  92.           dragging after one of these extends selection
  93.             by character, word, line repectively; anchor point is original click,
  94.             active end is the other end of the selection
  95.           after drag, active end can be moved with shift clicks or shift arrow keys
  96.           anchor and active ends of selection can be swapped
  97.             with option+shift+click, or option+shift+up/down arrow keys
  98.             
  99.     scrolling: holding down command, shift, option, and control keys
  100.                 speeds up scrolling when mousing in the arrows of the scroll bar
  101.                 speed is 2^^count lines per iteration,
  102.                  where count is a count of the pressed keys
  103. */
  104.  
  105.  
  106. #define OPTION_SPACE    '╩'                /* typed as OPTION <SPACE> - used for tabs */
  107. #define EMPTY_PTR        ((Ptr )2L)        /* valid but nonsense ptr used for Munger */
  108. #define PTL(a)           *((long *)&(a))    /* coerce Point or ChPos type to long */
  109.  
  110. #define        Abs(x)            ((x) < 0 ? -(x) : (x))
  111. #define        Max(x, y)        ((x) > (y) ? (x) : (y))
  112. #define        Min(x, y)        ((x) < (y) ? (x) : (y))
  113.  
  114. #define CARET_ON FALSE
  115. #define CARET_OFF TRUE
  116.  
  117. EventRecord            gLastMouseUp;
  118. EventRecord            gLastMouseDown;
  119. long                gMaxSleep;
  120.  
  121. static short         gClicks;
  122. static RgnHandle     gUtilRgn;
  123. static eRec          **gLastViewHit;
  124. static ChPos        zeroPos = { 0, 0 };
  125. /*
  126. #define    MAXINT        32767
  127. static Rect            gMobyRect = { -MAXINT, -MAXINT, MAXINT, MAXINT };
  128. */
  129. TextStyle dfltStylNormal =
  130. { monaco, normal,          0, 9, { 65536, 65536, 65536 } /* RGBColor Black */ };
  131. TextStyle dfltStylHilite =
  132. { monaco, bold+condense,   0, 9, { 65536, 65536, 65536 } /* RGBColor Black */ };
  133.  
  134.  
  135. static void eTeEdGuts( eRec **hE, Ptr textPtr, long numChars,
  136.                          Boolean hasReturn, Boolean show, short style );
  137. static void eTeFontChanged( eRec **hE );
  138. static void eTeHiliteRange( eRec **hE, register ChPos start, register ChPos stop );
  139. static void eTeAdjustScrollMax( eRec **hE );
  140. static void eTeCalibrate( eRec **hE );
  141. static short eTeOffsetToRun( eRec **hE, long anOffset );
  142. static short eTeRunToStyle( eRec **hE, short r );
  143. static ChPos eTePointHtoChPosH( eRec **hE, register Point aPt, register ChPos aPos );
  144. static ChPos eTePointToChPos( eRec **hE, register Point aPt );
  145. static Point eTeChPosToPoint( eRec **hE, register ChPos aPos );
  146. static void eTeDoHscroll( eRec **hE, short whichPart );
  147. static void eTeDoVscroll( eRec **hE, short whichPart );
  148. static Boolean eTeAutoScroll( eRec **hE, Point mouseLoc );
  149. static pascal void hSBarActionProc( ControlHandle macControl, short whichPart );
  150. static pascal void vSBarActionProc( ControlHandle macControl, short whichPart );
  151. /* obsolete...
  152. void     eTeSetFontNumber( eRec **hE, short aFontNumber );
  153. void     eTeSetFontName( eRec **hE, Str255 aFontName );
  154. void     eTeSetFontSize( eRec **hE, short aFontSize );
  155. */
  156. static short eTeUpdateLineStarts( eRec **hE, short firstLine );
  157. static void eTeKeyIns( eRec **hE, char c, short style );
  158.  
  159. /* the scrap  13Aug92  e  */
  160. static Handle eTeScrap;
  161. static long   eTeScrapLen;
  162. static long   eTeScrapCnt;
  163.  
  164. /* undo       13Aug92  e  */
  165.  
  166. typedef struct undoStuff
  167. {    unsigned char *undoTitle;            /* string for Edit menu */
  168.     void  (*undoProc)( eRec  **hE );    /* proc to do undo */
  169.     Handle    undoText;                    /* text that was wiped by last operation */
  170.     long    undoTxLen;                    /* size of undoText */
  171.     long    undoInLen;                    /* number of chars inserted since last delete */
  172.     eRec  **undoTeRec;                    /* eRec       of last operation */
  173.     long    undoStart;                    /* selStart   before last operation */
  174.     long    undoEnd;                    /* selEnd     before last operation */
  175.     Boolean undoDirty;                    /* dirty      before last operation */
  176.     Boolean undoKeyAccum;                /* can accumulate keystrokes */
  177.     Boolean undoDeleting;                /* some version of delete was last operation */
  178.     Boolean undoCarStaP;                /* TRUE if CaretChPos == selStart */
  179. } undoStuff;
  180.  
  181. static undoStuff eTeUndoStuff;
  182.  
  183. unsigned char *utUndo   = "\pUndo";
  184. unsigned char *utCopy   = "\pUndo Copy";
  185. unsigned char *utCut    = "\pUndo Cut";
  186. unsigned char *utPaste  = "\pUndo Paste";
  187. unsigned char *utClear  = "\pUndo Clear";
  188. unsigned char *utTyping = "\pUndo Typing";
  189. unsigned char *utInsert = "\pUndo Insert";
  190. unsigned char *utKill   = "\pUndo Kill";
  191.  
  192. unsigned char *rtCopy   = "\pRedo Copy";
  193. unsigned char *rtCut    = "\pRedo Cut";
  194. unsigned char *rtPaste  = "\pRedo Paste";
  195. unsigned char *rtClear  = "\pRedo Clear";
  196. unsigned char *rtTyping = "\pRedo Typing";
  197. unsigned char *rtInsert = "\pRedo Insert";
  198. unsigned char *rtKill   = "\pRedo Kill";
  199.  
  200. /* DeTabifyHandle takes a text handle,
  201.     and a size (which it assumes is less than or equal to the HandleSize)
  202.      removes all characters in the range 0..0x1f except RETURN (0x0d)
  203.      replacing TAB characters (0x09) with spaces according to tabstop
  204.      it makes two passes over the text,
  205.       and only one resize call to the memory manager
  206.      it returns the number of lines in count (passed by reference)
  207.      and an OsErr result  ( 0 == noErr )
  208. */
  209. /* static */
  210. short DeTabifyHandle( Handle h, long *size, long *count, short tabstop )
  211. { long old_size = *size;
  212.   long new_size = *size;
  213.   long line_count = 0;
  214.   register unsigned char *start = *(unsigned char **)h;
  215.   register unsigned char *end = &start[old_size-1];
  216.   register unsigned char *p = start;
  217.   register unsigned char *q = start;
  218.   register unsigned char c;
  219.   short error;
  220.   char lastch = *end;
  221.   
  222.   *end++ = '\0';
  223.   while ( p < end )
  224.   { c = *p++;
  225.     if ( c < ' ' )
  226.     { if ( c == '\r' )
  227.       { line_count++;
  228.         *q++ = c;
  229.         start = q;
  230.       }
  231.       else if ( c == '\t' )
  232.       { c = tabstop - ( ( q - start ) % tabstop);
  233.         *q++ = c;
  234.         c -= 1;
  235.         start -= c;
  236.         new_size += c;
  237.       }
  238.       else if ( p == end )
  239.       {  *q++ = lastch; /* the last char may be a control char */
  240.       }
  241.       else
  242.       { old_size -= 1;
  243.         new_size -= 1;
  244.       }
  245.     }
  246.     else
  247.       *q++ = c;
  248.   }
  249.   *count = line_count;
  250.   *size = new_size;
  251.   SetHandleSize( h, new_size );
  252.   asm { move.w D0, error }         /* error = MemError(); */
  253.   if ( error == noErr )
  254.   { end = *(unsigned char **)h;
  255.     p = &end[new_size];
  256.     *--p = lastch;
  257.     start = &end[old_size-1];
  258.     while ( p > end )
  259.     { c = *--start;
  260.       if ( c < ' ' && c != '\r' )
  261.         while ( c-- ) *--p = ' ';
  262.       else
  263.         *--p = c;
  264.     }
  265.   }
  266.   else
  267.   { /* cleanup the buffer? */
  268.   }
  269.   return error;
  270. }
  271.  
  272. static void undoBugNi( eRec **hE )
  273. {    SysBeep(3); SysBeep(10); SysBeep(3);
  274. }
  275.  
  276. void eTeInit( void )
  277. {
  278.     gUtilRgn = NewRgn();
  279.     gClicks = 0;
  280.     /* 13Aug92  e  */
  281.     eTeScrap = NewHandle(0);
  282.     eTeScrapLen = 0;
  283.     eTeScrapCnt = (InfoScrap())->scrapCount - 1;
  284.     eTeGetScrap();
  285.     /* */
  286.     eTeUndoStuff.undoProc = undoBugNi;
  287.     eTeUndoStuff.undoTitle = utUndo;
  288.     eTeUndoStuff.undoText = NewHandle(0);
  289.     eTeUndoStuff.undoTeRec = NULL;
  290. }
  291.  
  292. /* replacement can be installed via eTeSetWordBreak() */
  293.  
  294. static long WordLimits( char *text, long offset, Boolean reverse )
  295. {
  296.     register char *ptr, c;
  297.  
  298.     ptr = text + offset;
  299.     if ( reverse ) {
  300.         /* Scan backwards until we find beginning of word */
  301.         while ( ptr > text ) {
  302.             c = *( ptr - 1 );
  303.             if ( ( c >= 'a' && c <= 'z' ) ||
  304.                  ( c >= 'A' && c <= 'Z' ) || 
  305.                  ( c >= '0' && c <= '9' ) || 
  306.                  ( c == '_' ) )
  307.                 --ptr;
  308.             else
  309.                 break;
  310.         }
  311.     }
  312.     else {
  313.         /* Scan forwards until we find end of word */
  314.         while ( 1 ) {
  315.             c = *ptr;
  316.             if ( ( c >= 'a' && c <= 'z' ) ||
  317.                  ( c >= 'A' && c <= 'Z' ) || 
  318.                  ( c >= '0' && c <= '9' ) || 
  319.                  ( c == '_' ) )
  320.                 ++ptr;
  321.             else
  322.                 break;
  323.         }
  324.     }
  325.     return( ptr - text );
  326. }
  327.  
  328. static short eTeNewRuns( eRec **hE, long len )
  329. {    register long *runPtr;
  330.     register eRec *pE;
  331.     short error;
  332.  
  333.     if ( (**hE).hRuns )
  334.         DisposHandle( (Handle )(**hE).hRuns );
  335.     (**hE).hRuns = (long **)NewHandle( MIN_RUNS * sizeof( long ) );
  336.       asm { move.w D0, error }         /* error = MemError(); */
  337.       if( error == noErr )
  338.       {    pE = *hE;
  339.         runPtr = *((*pE).hRuns);
  340.         *runPtr++ = 0L;
  341.         *runPtr = len;
  342.         (*pE).runsAllocated = MIN_RUNS;
  343.         (*pE).qRuns = 1;
  344.         (*pE).fustStyle = 0;
  345.     }
  346.     return error;
  347. }
  348.  
  349. long eTeTextLength( eRec **hE )
  350. {
  351.     return (*(**hE).hRuns)[(**hE).qRuns];
  352. }
  353.  
  354. eRec **eTeNew( WindowPtr macPort, Rect viewRect, short tabStops, short wrap,
  355.                  short autoInd, ControlHandle aHSizing, ControlHandle aVSizing )
  356. {    
  357.     register eRec *pE;
  358.     LineRec *linePtr;
  359.     eRec   **hE = (eRec **)NewHandle( sizeof( eRec ) );
  360.  
  361.     if( hE != 0 )
  362.     {    HLock( (Handle )hE );
  363.         pE = *hE;
  364.         (*pE).active = FALSE;
  365.         (*pE).macPort = macPort;
  366.         (*pE).viewRect = viewRect;
  367.         (*pE).width = viewRect.right - viewRect.left;
  368.         (*pE).height = viewRect.bottom - viewRect.top;
  369.         (*pE).hOrigin = -viewRect.left;
  370.         (*pE).vOrigin = -viewRect.top;
  371.         (*pE).position.h = (*pE).position.v = 0;
  372.         (*pE).hScale = (*pE).vScale = 1;
  373.         (*pE).bounds.v = 1;
  374.         (*pE).bounds.h = 1;     /* arbitrary size for now */
  375.         (*pE).leftMargin = 1;    /* room for cursor will blink at the far left */
  376.         (*pE).topMargin = 0;
  377.         (*pE).selStart.h = (*pE).selStart.v = 0;
  378.         (*pE).selEnd = (*pE).selStart;
  379.         (*pE).selActive = FALSE;
  380.         (*pE).caretChPos.h = (*pE).caretChPos.v = 0;
  381.         (*pE).writeChPos.h = (*pE).writeChPos.v = 0;
  382.         (*pE).caretState = CARET_OFF;
  383.         (*pE).maxRight = 0;
  384.         (*pE).eTeWordBreak = WordLimits;
  385.         SetPort( macPort );
  386.         (*pE).tabStops = tabStops;
  387.         (*pE).spaceWidth = 1;
  388.         (*pE).wrap = wrap;                            /*  5Jul92  e  */
  389.         (*pE).autoInd = autoInd;
  390.         (*pE).hText = NewHandle( 1L );
  391.         **((*pE).hText) = '\0';
  392.         (*pE).hLines = (LineRec **)NewHandle( MIN_LINES * sizeof( LineRec ) );
  393.         linePtr = *((*pE).hLines);
  394.         *linePtr++ = 0L;
  395.         *linePtr = 0L;
  396.         (*pE).linesAllocated = MIN_LINES;
  397.         /* 2May92  e  */
  398.         (*pE).hRuns = NULL;
  399.         eTeNewRuns( hE, 0L);
  400.         (*pE).qRuns = 0;
  401.         (*pE).hPrint = NULL;                        /* 28Sep92  e  */
  402.         /* scrollPane */
  403.         (*pE).hStep = (*pE).vStep = 1;
  404.         (*pE).hOverlap = (*pE).vOverlap = 1;
  405.         (*pE).hContext = (*pE).vContext = CONTEXT_LINES;
  406.         if ( ( (*pE).hSBar = aHSizing ) != NULL )
  407.         {    SetCRefCon( aHSizing, (long )hE );
  408.             SetCtlAction( aHSizing, hSBarActionProc );
  409.             /* SetThumbFunc( aHSizing, SBarThumbFunc ); */
  410.         }
  411.         if ( ( (*pE).vSBar = aVSizing ) != NULL )
  412.         {    SetCRefCon( aVSizing, (long )hE );
  413.             SetCtlAction( aVSizing, vSBarActionProc );
  414.             /* SetThumbFunc( aVSizing, SBarThumbFunc ); */
  415.         }
  416.         HUnlock( (Handle )hE );
  417.         eTeSetStyles( hE, &dfltStylNormal, &dfltStylHilite);
  418.         eTeAdjustScrollMax( hE );
  419.         eTeCalibrate( hE );
  420.         (**hE).dirty = FALSE;  /* was (*pE). 14Jul92  e  */
  421.         (**hE).active = TRUE;  /* was (*pE). 14Jul92  e  */
  422.     }
  423.     return hE;
  424. }
  425.  
  426. void eTeDispose( eRec **hE )
  427. {
  428.     if ( (**hE).hText )
  429.         DisposHandle( (**hE).hText );
  430.     (**hE).hText = NULL;
  431.     if ( (**hE).hLines )
  432.         DisposHandle( (Handle )(**hE).hLines );
  433.     (**hE).hLines = NULL;
  434.     if ( (**hE).hRuns )
  435.         DisposHandle( (Handle )(**hE).hRuns );
  436.     (**hE).hRuns = NULL;
  437.     if ( (**hE).hPrint )
  438.         DisposHandle( (Handle)(**hE).hPrint );    /*  28Sep92  e  */
  439.     DisposHandle( (Handle )hE );
  440. }
  441.  
  442. static void eTePrepare( eRec **hE )
  443. {
  444.     Rect        tempRect;                /* ClipRect may move memory    */
  445.  
  446.     SetPort( (**hE).macPort );
  447.     tempRect = (**hE).viewRect;
  448.     ClipRect(&tempRect);
  449.     /* obsolete...
  450.     TextFont( (**hE).fontNumber );
  451.     TextSize( (**hE).fontSize );
  452.     TextFace( 0 ); */
  453.     /* Paranoid... */
  454.     TextMode( srcOr );
  455. }
  456.  
  457. static void eTePrepareStyle( eRec **hE, short style )
  458. {
  459.   if( (**hE).curStyle != style )
  460.   { (**hE).curStyle = style;
  461.     TextFont( (**hE).style[style].tsFont );
  462.     TextFace( (**hE).style[style].tsFace );
  463.     TextSize( (**hE).style[style].tsSize );
  464.     /* RGBColor tsColor; */
  465.   }
  466. }
  467.  
  468. static void eTePrepareRun( eRec **hE, short run )
  469. {
  470.     eTePrepareStyle( hE, eTeRunToStyle( hE, run ) );
  471. }
  472.  
  473. static void eTeRefresh( eRec **hE )
  474. {
  475.     SetPort( (**hE).macPort );
  476.     InvalRect( &(**hE).viewRect );
  477. }
  478.  
  479. static void eTeUpdateCaretRect( eRec **hE )
  480. {
  481.     Point    tempPt;
  482.     register eRec    *pE;
  483.  
  484.     tempPt = eTeChPosToPoint( hE, (**hE).caretChPos );
  485.     pE = *hE;
  486.     (*pE).caretRect.top    = tempPt.v;
  487.     (*pE).caretRect.right  = tempPt.h;
  488.     (*pE).caretRect.left   = tempPt.h - 1;
  489.     (*pE).caretRect.bottom = tempPt.v + (*pE).caretHeight;
  490. }
  491.  
  492. void eTeSetWrap( eRec **hE, short wrap )
  493. {
  494.     /* 22Jul92  e  -- added limits */
  495.     if( wrap < 1 ) wrap = 1;
  496.     else if( wrap > 999 ) wrap = 999;
  497.     /* (**hE).wrap = wrap; */
  498.     /* 10Aug92  e   need to fix all ChPos for new wrap! */
  499.     if( (**hE).wrap != wrap )
  500.     { long car = eTeChPosToOffset( hE, (**hE).caretChPos );
  501.       long sta = eTeChPosToOffset( hE, (**hE).selStart );
  502.       long end = eTeChPosToOffset( hE, (**hE).selEnd );
  503.       long wri = eTeChPosToOffset( hE, (**hE).writeChPos );
  504.       (**hE).wrap = wrap;
  505.       eTeUpdateLineStarts( hE, 0 );
  506.       (**hE).caretChPos = eTeOffsetToChPos( hE, car );
  507.       (**hE).selStart   = eTeOffsetToChPos( hE, sta );
  508.       (**hE).selEnd     = eTeOffsetToChPos( hE, end );
  509.       (**hE).writeChPos = eTeOffsetToChPos( hE, wri );
  510.       eTeUpdateCaretRect( hE );
  511.       eTeRefresh( hE );
  512.     }
  513. }
  514.  
  515. void eTeSetTabStop( eRec **hE, short aTabStop )
  516. {
  517.     /* Undo? */
  518.     (**hE).tabStops = aTabStop;
  519.     (**hE).tabWidth = aTabStop * (**hE).spaceWidth;
  520.     eTeRefresh( hE );
  521. }
  522.  
  523. static short eTeTabStop( eRec **hE, register short curPosition )
  524. {
  525.     register short tabWidth = (**hE).tabWidth;
  526.     return (curPosition +
  527.              (tabWidth -
  528.                ((curPosition - (**hE).leftMargin + (**hE).hOrigin) % tabWidth)));
  529. }
  530.  
  531. void eTeSetWordBreak( eRec **hE, ProcPtr aFunc )
  532. {
  533.     if ( aFunc == NULL )
  534.         (**hE).eTeWordBreak = WordLimits;
  535.     else
  536.         (**hE).eTeWordBreak = aFunc;
  537. }
  538.  
  539. /* 23Jul92  e  */
  540.  
  541. /* Inverts the text between the start and stop positions. */
  542.  
  543. static void eTeHiliteRange( eRec **hE, register ChPos start, register ChPos stop )
  544. {
  545.     Rect tmpRect;
  546.     Point stopPt;
  547.     register short vScale;
  548.     
  549.     if ( ! (**hE).active )
  550.         return;
  551.  
  552.     topLeft( tmpRect ) = eTeChPosToPoint( hE, start );
  553.     tmpRect.left -= 1;
  554.  
  555.     vScale = (**hE).vScale;
  556.  
  557.     /* Just need to hilite within same line */
  558.     if ( start.v == stop.v ) {
  559.         botRight( tmpRect ) = eTeChPosToPoint( hE, stop );
  560.         tmpRect.right -= 1;
  561.         tmpRect.bottom = tmpRect.top + vScale;
  562.         if ( tmpRect.left != tmpRect.right ) {
  563.             eTePrepare( hE );
  564.             asm { bclr #hiliteBit, HiliteMode }
  565.             InvertRect( &tmpRect );
  566.         }
  567.     }
  568.     /* Hilite spans more than one line */
  569.     else
  570.     {    stopPt = eTeChPosToPoint( hE, stop );
  571.         if( tmpRect.top < (**hE).viewRect.bottom && stopPt.v >= (**hE).viewRect.top )
  572.         {    eTePrepare( hE );
  573.             tmpRect.right = (**hE).viewRect.right;
  574.             tmpRect.bottom = tmpRect.top + vScale;
  575.             if( tmpRect.bottom > (**hE).viewRect.top )
  576.             {    asm { bclr #hiliteBit, HiliteMode }
  577.                 InvertRect( &tmpRect );
  578.                 tmpRect.top += vScale;
  579.             }
  580.             else
  581.                 tmpRect.top = (**hE).viewRect.top;
  582.             tmpRect.left = (**hE).viewRect.left;
  583.             tmpRect.bottom = Min( stopPt.v, (**hE).viewRect.bottom );
  584.             asm { bclr #hiliteBit, HiliteMode }
  585.             InvertRect( &tmpRect );
  586.             if( stopPt.v < (**hE).viewRect.bottom )
  587.             {    tmpRect.bottom += vScale;
  588.                 tmpRect.top = stopPt.v;
  589.                 tmpRect.right = stopPt.h - 1;
  590.                 asm { bclr #hiliteBit, HiliteMode }
  591.                 InvertRect( &tmpRect );
  592.             }
  593.         }
  594.     }
  595. }
  596.  
  597. /* 23Jul92  e  */
  598.  
  599. static void eTeSetCaretState( eRec **hE, Boolean state )
  600. {
  601.     Rect    tmpRect;
  602.     register eRec    *pE = *hE;
  603.  
  604.     if (   ! (*pE).selActive
  605.           && (*pE).active
  606.           && (*pE).caretState != state )
  607.     {
  608.         (*pE).caretState = state;
  609.         tmpRect = (*pE).caretRect;
  610.         eTePrepare( hE );
  611.         PenMode(patXor);
  612.         FrameRect( &tmpRect );
  613.         PenNormal();
  614.     }
  615. }
  616.  
  617. void eTeActivate( eRec **hE )
  618. {
  619.     if( (**hE).active == FALSE )
  620.     {    (**hE).active = TRUE;
  621.         if( (**hE).selActive )
  622.             eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );
  623.     }
  624. }
  625.  
  626. void eTeDeactivate( eRec **hE )
  627. {
  628.     if( (**hE).active == TRUE )
  629.     {    if( (**hE).selActive )
  630.             eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );
  631.         else
  632.             eTeSetCaretState( hE, CARET_OFF );
  633.         (**hE).active = FALSE;
  634.     }
  635. }
  636.  
  637. void eTeIdle( eRec **hE )
  638. {
  639.     static long lastCaretToggle = 0L;
  640.     register long    now;
  641.     Rect    tmpRect;
  642.  
  643.     gMaxSleep = GetCaretTime();    /* user may change it using desk accessory */
  644.     now = TickCount();
  645.     if ( now - lastCaretToggle >= gMaxSleep ) {
  646.         lastCaretToggle = now;
  647.         eTeSetCaretState( hE, (**hE).caretState == CARET_OFF ? CARET_ON : CARET_OFF );
  648.     }
  649.     else {
  650.         gMaxSleep -= ( now - lastCaretToggle );
  651.     }
  652. }
  653.  
  654. static void eTeResize( eRec **hE, register Rect *delta )
  655. {
  656.     register eRec *pE = *hE;
  657.  
  658.     (*pE).width      += delta->right - delta->left;
  659.     (*pE).height     += delta->bottom - delta->top;
  660.     (*pE).leftMargin += delta->left;
  661.     (*pE).topMargin  += delta->top;
  662.  
  663.     (*pE).hOrigin -= delta->left;
  664.     (*pE).vOrigin -= delta->top;
  665.  
  666.     eTeUpdateCaretRect( hE );
  667.     eTeAdjustScrollMax( hE );
  668.     eTeCalibrate( hE );
  669. }
  670.  
  671. void eTeNewView( eRec **hE, Rect *viewRect )
  672. {
  673.     register eRec *pE = *hE;
  674.     Rect    delta;
  675.     
  676.     delta = *viewRect;
  677.     delta.top -= (*pE).viewRect.top;
  678.     delta.left -= (*pE).viewRect.left;
  679.     delta.bottom -= (*pE).viewRect.bottom;
  680.     delta.right -= (*pE).viewRect.right;
  681.     (*pE).viewRect   = *viewRect;
  682.     
  683.     eTeResize( hE, &delta );
  684. }
  685.  
  686. /* eTeSetStyles also causes the display to be redrawn */
  687.  
  688. void eTeSetStyles( eRec **hE, TextStyle *ts0, TextStyle *ts1 )
  689. {
  690.     short        height[2];
  691.     FontInfo    fontInfo[2];
  692.     ChPos        tmpPt;
  693.     short        bigger;
  694.  
  695.     (**hE).style[0] = *ts0;
  696.     (**hE).style[1] = *ts1;
  697.     (**hE).curStyle = NOSTYLE;
  698.  
  699.     /* Turn off cursor */
  700.     eTePrepare( hE );
  701.     eTeSetCaretState( hE, CARET_OFF );
  702.  
  703.     eTePrepareStyle( hE, 1 );
  704.     GetFontInfo( &fontInfo[1] );
  705.     height[1] = CharWidth( OPTION_SPACE );
  706.  
  707.     eTePrepareStyle( hE, 0 );
  708.     GetFontInfo( &fontInfo[0] );
  709.     height[0] = CharWidth( OPTION_SPACE );
  710.     
  711.     (**hE).spaceWidth = Max ( height[0], height[1] );
  712.     /* calculate new tabstop based on new font/size */
  713.     (**hE).tabWidth = (**hE).tabStops * (**hE).spaceWidth;
  714.  
  715.     height[0] = fontInfo[0].ascent + fontInfo[0].descent;
  716.     height[1] = fontInfo[1].ascent + fontInfo[1].descent;
  717.     
  718.     bigger = height[1] > height[0] ? 1 : 0;
  719.     (**hE).fontAscent = fontInfo[bigger].ascent;
  720.     (**hE).caretHeight = height[bigger];
  721.  
  722.     /* Update caret values for new font */
  723.     eTeUpdateCaretRect( hE );
  724.     (**hE).maxRight = (**hE).caretRect.right;
  725.  
  726.     /*
  727.      * Set the scale values for our scrollbars. Vertical scrolling is done by 
  728.      * lines; horizontal by widest character. First, move to top of panorama (but
  729.      * don't redraw), then change the scale values. Finally, move back to original
  730.      * position and redraw screen.
  731.      */
  732.     tmpPt = (**hE).position;
  733.     eTeScroll( hE, -(**hE).position.h, -(**hE).position.v, FALSE );
  734.     (**hE).hScale = Max( fontInfo[0].widMax, fontInfo[1].widMax );
  735.     (**hE).vScale = (**hE).caretHeight + fontInfo[bigger].leading;
  736.     eTeAdjustScrollMax( hE );
  737.     eTeScrollTo( hE, tmpPt, FALSE );
  738.     eTeRefresh( hE );
  739. }
  740.  
  741. #ifdef include_obsolete_code
  742.  
  743. void eTeSetFontNumber( eRec **hE, short aFontNumber )
  744. {
  745.     Str255        fontName;
  746.  
  747.     GetFontName( aFontNumber, fontName );
  748.     if ( fontName[0] == 0 )
  749.         aFontNumber = 0;    /* If font does not exist, then use the system font */
  750.  
  751.     /* Undo? */
  752.     (**hE).fontNumber = aFontNumber;
  753.     eTeFontChanged( hE );
  754. }
  755.  
  756. void eTeSetFontName( eRec **hE, Str255 aFontName )
  757. {
  758.     short    fontNumber;
  759.  
  760.     GetFNum( aFontName, &fontNumber );
  761.     eTeSetFontNumber( hE, ( fontNumber > 0 ) ? fontNumber : 0 );
  762. }
  763.  
  764. void eTeSetFontSize( eRec **hE, short aFontSize )
  765. {
  766.     /* Undo? */
  767.     (**hE).fontSize = aFontSize;
  768.     eTeFontChanged( hE );
  769. }
  770.  
  771. #endif
  772.  
  773. /* static --  25Sep92  e  for os_mac_Print.c */
  774. void eTeDrawLine( eRec **hE, ChPos beginPos, Point location )
  775. {
  776.     register char    c, *charPtr, *firstChar;
  777.     register short    count;
  778.     long     offset, instyle, inline, eol;
  779.     short     run, style;
  780.     Point    pt;
  781.  
  782.     HLock( (**hE).hText );
  783.     MoveTo( location.h, location.v + (**hE).fontAscent );
  784.     
  785.     offset = *(*(**hE).hLines + beginPos.v) + beginPos.h;
  786.     charPtr = *(**hE).hText + offset;
  787.     eol    = *(*(**hE).hLines + beginPos.v + 1);
  788.     inline = eol - offset;
  789.     run = eTeOffsetToRun( hE, offset );
  790.     while( inline )
  791.     {    eTePrepareRun( hE, run );
  792.         instyle = (*(**hE).hRuns)[++run] - offset;
  793.         if( instyle > inline )
  794.             instyle = inline;
  795.         inline -= instyle;
  796.         offset += instyle;
  797.         firstChar = charPtr;
  798.         count = 0;
  799.         while ( instyle && ( c = *charPtr++ ) != RETURN )    /* 11Aug92  e   was: instyle-- */
  800.         {    if ( c == TAB ) 
  801.             {    if ( count > 0 ) DrawText( firstChar, 0, count );
  802.                 GetPen( &pt );
  803.                 pt.h = eTeTabStop( hE, pt.h );
  804.                 MoveTo( pt.h, pt.v );
  805.                 firstChar = charPtr;
  806.                 count = 0;
  807.             }
  808.             else ++count;
  809.             instyle -= 1;                                    /* 11Aug92  e  */
  810.         }
  811.         if ( count  > 0 ) DrawText( firstChar, 0, count );
  812.     }
  813.     if( c != RETURN && *charPtr != '\0' ) DrawChar( 0xd7 );
  814.     /* if( instyle == 0 && *charPtr != '\0' ) DrawChar( 0xd7 ); /* NG!? 11Aug92  e  */
  815.     HUnlock( (**hE).hText );
  816. }
  817.  
  818. void eTeDraw( eRec **hE, Rect *area )
  819. {
  820.     eRec *pE;
  821.     short    vFirst, vLast;
  822.     ChPos    startPos;
  823.     Point    location;
  824.     Rect    tRect;
  825.     Boolean doCaret;    /* 23Jul92  e */
  826.     
  827.     HLock( (Handle )hE );
  828.     pE = *hE;
  829.     
  830.     if ( SectRect( area, &(*pE).viewRect, &tRect ) )
  831.     {
  832.         eTePrepare( hE );
  833.         
  834.         doCaret = ( ( ! (*pE).selActive ) && ( (*pE).caretState == CARET_ON ) );
  835.         if( doCaret ) eTeSetCaretState( hE, CARET_OFF );
  836.  
  837.         vFirst = ( tRect.top - (*pE).topMargin + (*pE).vOrigin ) / (*pE).vScale;
  838.         vLast = ( tRect.bottom + (*pE).vScale - (*pE).topMargin + (*pE).vOrigin - 1 )
  839.                 / (*pE).vScale;
  840.  
  841.         if ( vFirst < 0 )
  842.             vFirst = 0;
  843.         if ( vLast >= (*pE).bounds.v ) 
  844.             vLast = (*pE).bounds.v - 1;
  845.  
  846.         EraseRect( &tRect );
  847.  
  848.         if ( vFirst < (*pE).bounds.v && vLast >= 0)
  849.  
  850.         {    location.h = (*pE).leftMargin - (*pE).hOrigin;
  851.             location.v = vFirst * (*pE).vScale + (*pE).topMargin - (*pE).vOrigin;
  852.             startPos.h = 0;
  853.  
  854.             for ( startPos.v = vFirst;
  855.                   startPos.v <= vLast;
  856.                   location.v += (*pE).vScale, ++startPos.v )
  857.             {    eTeDrawLine( hE, startPos, location );
  858.             }
  859.             if ( (*pE).selActive )
  860.                  eTeHiliteRange( hE, (*pE).selStart, (*pE).selEnd );    /* may have munged it */
  861.             else if( doCaret )
  862.                 eTeSetCaretState( hE, CARET_ON );
  863.         }
  864.     }
  865.     HUnlock( (Handle )hE );
  866. }
  867.  
  868. void eTeUpdate( eRec **hE )
  869. {
  870.     Rect tempRect;
  871.     
  872.     eTePrepare( hE );
  873.     tempRect = (**((**hE).macPort)->visRgn).rgnBBox;
  874.     eTeDraw( hE, &tempRect );
  875. }
  876.         
  877. #ifndef THINK_ARROWS
  878.  
  879. static void eTeExtSelGuts( eRec **hE, ChPos chPos )
  880. {
  881.     if( PTL( chPos ) < PTL( (**hE).caretChPos ) )
  882.     {    eTeHiliteRange( hE, chPos, (**hE).caretChPos );
  883.         if( PTL( (**hE).caretChPos ) == PTL( (**hE).selStart ) )
  884.             (**hE).selStart = chPos;
  885.         else if( PTL( chPos ) >= PTL( (**hE).selStart ) )
  886.             (**hE).selEnd = chPos;
  887.         else
  888.         {    (**hE).selEnd = (**hE).selStart;
  889.             (**hE).selStart = chPos;
  890.         }
  891.     }
  892.     else if( PTL( chPos ) > PTL( (**hE).caretChPos ) )
  893.     {    eTeHiliteRange( hE, (**hE).caretChPos, chPos );
  894.         if( PTL( (**hE).caretChPos ) == PTL( (**hE).selEnd ) )
  895.             (**hE).selEnd = chPos;
  896.         else if( PTL( chPos ) <= PTL( (**hE).selEnd ) )
  897.             (**hE).selStart = chPos;
  898.         else
  899.         {    (**hE).selStart = (**hE).selEnd;
  900.             (**hE).selEnd = chPos;
  901.         }
  902.     }
  903. }
  904.  
  905. #endif
  906.  
  907. static void eTeEnsureChPos( eRec **hE, ChPos chPos )
  908. {
  909.     if( PTL( chPos ) != PTL( (**hE).caretChPos ) )
  910.     {    (**hE).caretChPos = chPos;
  911.         eTeUpdateCaretRect( hE );
  912.         (**hE).maxRight = (**hE).caretRect.right;
  913.     }
  914. }
  915.  
  916. void eTeKey( eRec **hE, char theChar, Byte keyCode, short modifiers, short style )
  917. {
  918.     char        *charPtr;
  919.     LineRec        *linePtr;
  920.     long        offset;
  921.     short            eoln;
  922.     ChPos        chPos;
  923.     char        chars[ 255 ];
  924.     Boolean        shifted, optioned, commanded;
  925.     Rect         invalRect;
  926.     long        new;
  927.     Point         aPt;
  928.  
  929.     ObscureCursor();
  930.     eTeSetCaretState( hE, CARET_OFF ); /* move up here 22Jul92  e  */
  931.  
  932.     shifted = ( modifiers & shiftKey ) ? 1 : 0;
  933.     optioned = ( modifiers & optionKey ) ? 1 : 0;
  934.     commanded = ( modifiers & cmdKey ) ? 1 : 0;
  935.  
  936.     switch ( keyCode ) {
  937.         case KeyF1:
  938.             /* Undo */
  939.             break;
  940.         case KeyF2:
  941.             eTeCut( hE );
  942.             break;
  943.         case KeyF3:
  944.             eTeCopy( hE );
  945.             break;
  946.         case KeyF4:
  947.             /* eTePaste( hE, style );            /* correct        3May92  e */
  948.             eTePaste( hE, style ^ optioned );    /* for debugging  3May92  e */
  949.             break;
  950.         case KeyF5:
  951.         case KeyF6:
  952.         case KeyF7:
  953.         case KeyF8:
  954.         case KeyF9:
  955.         case KeyF10:
  956.         case KeyF11:
  957.         case KeyF12:
  958.         case KeyF13:
  959.         case KeyF14:
  960.         case KeyF15:
  961.             /* DoFunctionKey( theChar, keyCode, modifiers ); */
  962.             break;
  963.  
  964.         case KeyHome:                                            /*** HOME ***/
  965.             eTeScrollTo( hE, zeroPos, TRUE );
  966.             break;
  967.  
  968.         case KeyEnd:                                            /*** END ***/
  969.             chPos.v = (**hE).bounds.v - (**hE).vSpan;
  970.             if ( chPos.v < 0 )
  971.                 chPos.v = 0;
  972.             chPos.h = (**hE).position.h;
  973.             eTeScrollTo( hE, chPos, TRUE );
  974.             break;
  975.  
  976.         case KeyPageUp:                                            /*** PAGE UP ***/
  977.             eTeDoVscroll( hE, inPageUp );
  978.             eTeAdjustScrollMax( hE );
  979.             break;
  980.  
  981.         case KeyPageDown:                                        /*** PAGE DOWN ***/
  982.             eTeDoVscroll( hE, inPageDown );
  983.             eTeAdjustScrollMax( hE );
  984.             break;
  985.  
  986.         case KeyDel:                                            /*** DEL FWD ***/
  987.             offset = eTeChPosToOffset( hE, (**hE).caretChPos );
  988.             if ( (**hE).selActive )
  989.                 eTeDelete( hE );
  990.             else if ( optioned ) { /* delete to end of line */
  991.                 /* 5Jul92  e */
  992.                 chPos = (**hE).caretChPos;
  993.                 linePtr = *(**hE).hLines;
  994.               labelTryAgainK:
  995.                 chPos.h = linePtr[ chPos.v + 1 ] - linePtr[ chPos.v ];
  996.                 if ( chPos.v < (**hE).bounds.v - 1 )
  997.                 { --chPos.h;
  998.                   new = eTeChPosToOffset( hE, chPos );
  999.                   if( *(*(**hE).hText + new) != RETURN )
  1000.                   { chPos.v++;
  1001.                     goto labelTryAgainK;
  1002.                   }
  1003.                   if( new == offset )
  1004.                       chPos.h++;    /* delete the RETURN if its all there is */
  1005.                 }
  1006.                 eTeKillTo( hE, chPos );
  1007.             }
  1008.             else if ( *(*(**hE).hText + offset) != '\0' ) {
  1009.                 /* 14Aug92  e   for undo...
  1010.                 eTeEdGuts( hE, EMPTY_PTR, 0L, *(*(**hE).hText + offset) == RETURN, TRUE, style );
  1011.                 */
  1012.                 eTeKillTo( hE, eTeOffsetToChPos( hE, offset + 1 ) ); /* optimize for Undo Typing */
  1013.             }
  1014.             break;
  1015.  
  1016.         default:
  1017.             MoveHHi( (Handle )(**hE).hLines );
  1018.             HLock( (Handle )(**hE).hLines );
  1019.             /* eTeSetCaretState( hE, CARET_OFF );  --  move above  22Jul92  e  */
  1020.             linePtr = *(**hE).hLines;
  1021.             switch( theChar ) {
  1022.                 case LEFT_ARROW:                                /*** LEFT ARROW ***/
  1023.                     if ( (**hE).selActive && ! shifted )
  1024.                     {    /* if no shift key and there is a selection, remove it */
  1025.                         eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );
  1026.                         chPos = (**hE).selEnd = (**hE).selStart;
  1027.                     }
  1028. #ifndef THINK_ARROWS
  1029.                     else if( (**hE).selActive && optioned && commanded )
  1030.                     {    /* move chPos to start of selection if not there */
  1031.                         chPos = (**hE).selStart;
  1032.                         eTeEnsureChPos( hE, chPos );
  1033.                     }
  1034. #endif
  1035.                     else
  1036.                     {    if ( shifted )
  1037.                           {    if ( ! (**hE).selActive )
  1038.                                 (**hE).selStart = (**hE).selEnd = (**hE).caretChPos;
  1039. #ifdef THINK_ARROWS
  1040.                             (**hE).caretChPos = (**hE).selStart;
  1041. #endif
  1042.                         }
  1043.                         chPos = (**hE).caretChPos;
  1044.                         if ( commanded )
  1045.                         {    chPos.h = 0;    /* move to beginning of line */
  1046.                             labelTryAgainL:    /* added the rest 5Jul92  e  */
  1047.                             if ( chPos.v > 0 )
  1048.                             {    offset = eTeChPosToOffset( hE, chPos );
  1049.                                 if( *(*(**hE).hText + offset - 1) != RETURN )
  1050.                                 {    chPos.v--;
  1051.                                     goto labelTryAgainL;
  1052.                                 }
  1053.                             }
  1054.                         }
  1055.                         else if ( optioned )
  1056.                         {                     /* move to start/end of previous word */
  1057.                               offset = eTeChPosToOffset( hE, chPos );
  1058.                             new = WordLimits( *(**hE).hText, offset, TRUE );
  1059.                             if ( new == offset && offset != 0L )
  1060.                                 --new;
  1061.                             chPos = eTeOffsetToChPos( hE, new );
  1062.                         }
  1063.                           else                 /* move to previous character position */
  1064.                             chPos = eTeOffsetToChPos( hE, eTeChPosToOffset( hE, chPos ) - 1 );
  1065.                         if ( shifted )
  1066.                         {
  1067. #ifdef THINK_ARROWS
  1068.                             eTeHiliteRange( hE, chPos, (**hE).selStart );
  1069.                             (**hE).selStart = chPos;
  1070. #else
  1071.                             eTeExtSelGuts( hE, chPos );
  1072. #endif
  1073.                             /* 30Apr92  e  -- needed if newly active AND scrolling... */
  1074.                             (**hE).selActive = ( PTL( (**hE).selStart ) != PTL( (**hE).selEnd ) );
  1075.                         }
  1076.                     }
  1077.                     (**hE).caretChPos = chPos;
  1078.                     eTeUpdateCaretRect( hE );
  1079.                     (**hE).maxRight = (**hE).caretRect.right;
  1080.                     eTeShowCaret( hE );
  1081.                     break;
  1082.  
  1083.                 case RIGHT_ARROW:                                /*** RIGHT ARROW ***/
  1084.                     if ( (**hE).selActive && ! shifted )
  1085.                     {    /* if no shift key and there is a selection, remove it */
  1086.                         eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );
  1087.                         chPos = (**hE).selStart = (**hE).selEnd;
  1088.                     }
  1089. #ifndef THINK_ARROWS
  1090.                     else if( (**hE).selActive && optioned && commanded )
  1091.                     {    /* move chPos to end of selection if not there */
  1092.                         chPos = (**hE).selEnd;
  1093.                         eTeEnsureChPos( hE, chPos );
  1094.                     }
  1095. #endif
  1096.                     else
  1097.                     {    if ( shifted )
  1098.                           {    if ( ! (**hE).selActive )
  1099.                                 (**hE).selStart = (**hE).selEnd = (**hE).caretChPos;
  1100. #ifdef THINK_ARROWS
  1101.                             (**hE).caretChPos = (**hE).selEnd;
  1102. #endif
  1103.                         }
  1104.                         chPos = (**hE).caretChPos;
  1105.                         if ( commanded )
  1106.                         {                    /* move to end of line */
  1107.                             labelTryAgainR:
  1108.                             chPos.h = linePtr[ chPos.v + 1 ] - linePtr[ chPos.v ];
  1109.                             if ( chPos.v < (**hE).bounds.v - 1 )
  1110.                             {    /* 5Jul92  e  was: --chPos.h; */
  1111.                                 --chPos.h;
  1112.                                 offset = eTeChPosToOffset( hE, chPos );
  1113.                                 if( *(*(**hE).hText + offset) != RETURN )
  1114.                                 {    chPos.v++;
  1115.                                     goto labelTryAgainR;
  1116.                                 }
  1117.                             }
  1118.                         }
  1119.                         else if ( optioned )
  1120.                         {                     /* move to start/end of next word */
  1121.                               offset = eTeChPosToOffset( hE, chPos );
  1122.                             new = WordLimits( *(**hE).hText, offset, FALSE );
  1123.                             if ( new == offset && *(*(**hE).hText + offset) != '\0' )
  1124.                                 ++new;
  1125.                             chPos = eTeOffsetToChPos( hE, new );
  1126.                         }
  1127.                           else                 /* move to previous character position */
  1128.                             chPos = eTeOffsetToChPos( hE, eTeChPosToOffset( hE, chPos ) + 1 );
  1129.                         if ( shifted )
  1130.                         {
  1131. #ifdef THINK_ARROWS
  1132.                             eTeHiliteRange( hE, (**hE).selEnd, chPos );
  1133.                             (**hE).selEnd = chPos;
  1134. #else
  1135.                             eTeExtSelGuts( hE, chPos );
  1136. #endif
  1137.                             /* 30Apr92  e  -- needed if newly active AND scrolling... */
  1138.                             (**hE).selActive = ( PTL( (**hE).selStart ) != PTL( (**hE).selEnd ) );
  1139.                         }
  1140.                     }
  1141.                     (**hE).caretChPos = chPos;
  1142.                     eTeUpdateCaretRect( hE );
  1143.                     (**hE).maxRight = (**hE).caretRect.right;
  1144.                     eTeShowCaret( hE );
  1145.                     break;
  1146.  
  1147.                 case UP_ARROW:                                /*** UP ARROW ***/
  1148.                     if ( (**hE).selActive && ! shifted )
  1149.                     {    /* if no shift key and there is a selection, remove it */
  1150.                         eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );
  1151.                         chPos = (**hE).selEnd = (**hE).selStart;
  1152.                     }
  1153. #ifndef THINK_ARROWS
  1154.                     else if( (**hE).selActive && optioned && commanded )
  1155.                     {    /* move chPos to start of selection if not there */
  1156.                         chPos = (**hE).selStart;
  1157.                         eTeEnsureChPos( hE, chPos );
  1158.                     }
  1159. #endif
  1160.                     else
  1161.                     {    if ( shifted )
  1162.                           {    if ( ! (**hE).selActive )
  1163.                                 (**hE).selStart = (**hE).selEnd = (**hE).caretChPos;
  1164. #ifdef THINK_ARROWS
  1165.                             else
  1166.                                 eTeEnsureChPos( hE, (**hE).selStart );
  1167. #endif
  1168.                         }
  1169.                         chPos = (**hE).caretChPos;
  1170.                         if ( commanded )
  1171.                             chPos.h = chPos.v = 0;    /* move to beginning of text */
  1172.                         else if( optioned )
  1173.                         {    aPt.h = (**hE).maxRight;
  1174.                             chPos.v -= (**hE).vSpan - (**hE).vOverlap;    /* move up one page */
  1175.                             if( chPos.v < 0 ) chPos.v = 0;
  1176.                             chPos = eTePointHtoChPosH( hE, aPt, chPos );
  1177.                         }
  1178.                           else if ( chPos.v > 0 )
  1179.                         {    /* NG...   [caretRect.top can be bogus!]
  1180.                             aPt.h = (**hE).maxRight;
  1181.                             aPt.v = (**hE).caretRect.top - (**hE).vScale;
  1182.                             chPos = eTePointToChPos( hE, aPt );
  1183.                             */
  1184.                             aPt.h = (**hE).maxRight;
  1185.                             chPos.v -= 1;    /* move up one line */
  1186.                             chPos = eTePointHtoChPosH( hE, aPt, chPos );
  1187.                         }
  1188.                         if ( shifted )
  1189.                         {
  1190. #ifdef THINK_ARROWS
  1191.                             eTeHiliteRange( hE, chPos, (**hE).selStart );
  1192.                             (**hE).selStart = chPos;
  1193. #else
  1194.                             eTeExtSelGuts( hE, chPos );
  1195. #endif
  1196.                             /* 30Apr92  e  -- needed if newly active AND scrolling... */
  1197.                             (**hE).selActive = ( PTL( (**hE).selStart ) != PTL( (**hE).selEnd ) );
  1198.                         }
  1199.                     }
  1200.                     (**hE).caretChPos = chPos;
  1201.                     eTeUpdateCaretRect( hE );
  1202.                     eTeShowCaret( hE );
  1203.                     break;
  1204.  
  1205.                 case DOWN_ARROW:                            /*** DOWN ARROW ***/
  1206.                     if ( (**hE).selActive && ! shifted )
  1207.                     {    /* if no shift key and there is a selection, remove it */
  1208.                         eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );
  1209.                         chPos = (**hE).selStart = (**hE).selEnd;
  1210.                     }
  1211. #ifndef THINK_ARROWS
  1212.                     else if( (**hE).selActive && optioned && commanded )
  1213.                     {    /* move chPos to end of selection if not there */
  1214.                         chPos = (**hE).selEnd;
  1215.                         eTeEnsureChPos( hE, chPos );
  1216.                     }
  1217. #endif
  1218.                     else
  1219.                     {    if ( shifted )
  1220.                           {    if ( ! (**hE).selActive )
  1221.                                 (**hE).selStart = (**hE).selEnd = (**hE).caretChPos;
  1222. #ifdef THINK_ARROWS
  1223.                             else
  1224.                                 eTeEnsureChPos( hE, (**hE).selEnd );
  1225. #endif
  1226.                         }
  1227.                         chPos = (**hE).caretChPos;
  1228.                         if ( commanded )
  1229.                         {                    /* move to end of text */
  1230.                             chPos.v = (**hE).bounds.v - 1;
  1231.                             chPos.h = linePtr[ chPos.v + 1 ] - linePtr[ chPos.v ];
  1232.                         }
  1233.                         else if( optioned )
  1234.                         {    aPt.h = (**hE).maxRight;
  1235.                             if( chPos.v > (**hE).bounds.v - (**hE).vSpan + (**hE).vOverlap )
  1236.                                 chPos.v = (**hE).bounds.v - 1;
  1237.                             else
  1238.                                 chPos.v += (**hE).vSpan - (**hE).vOverlap;    /* move down one page */
  1239.                             chPos = eTePointHtoChPosH( hE, aPt, chPos );
  1240.                         }
  1241.                           else if ( chPos.v < (**hE).bounds.v - 1 )
  1242.                         {    /* NG...   [caretRect.top can be bogus!]
  1243.                             aPt.h = (**hE).maxRight;
  1244.                             aPt.v = (**hE).caretRect.top + (**hE).vScale;
  1245.                             chPos = eTePointToChPos( hE, aPt );
  1246.                             */
  1247.                             aPt.h = (**hE).maxRight;
  1248.                             chPos.v += 1;    /* move down one line */
  1249.                             chPos = eTePointHtoChPosH( hE, aPt, chPos );
  1250.                         }
  1251.                         if ( shifted )
  1252.                         {
  1253. #ifdef THINK_ARROWS
  1254.                             eTeHiliteRange( hE, (**hE).selEnd, chPos );
  1255.                             (**hE).selEnd = chPos;
  1256. #else
  1257.                             eTeExtSelGuts( hE, chPos );
  1258. #endif
  1259.                             /* 30Apr92  e  -- needed if newly active AND scrolling... */
  1260.                             (**hE).selActive = ( PTL( (**hE).selStart ) != PTL( (**hE).selEnd ) );
  1261.                         }
  1262.                     }
  1263.                     (**hE).caretChPos = chPos;
  1264.                     eTeUpdateCaretRect( hE );
  1265.                     eTeShowCaret( hE );
  1266.                     break;
  1267.  
  1268.                 case DELETE:                                /*** DELETE ***/
  1269.                     eTeDelete( hE );
  1270.                     break;
  1271.  
  1272.                 default:                                    /*** TEXT ***/
  1273.                     /* Undo? */
  1274.                     if ( theChar == ENTER || theChar == RETURN ) {
  1275.                         chars[ 0 ] = RETURN;
  1276.                         offset = 1;
  1277. #ifdef THINK_RETURN
  1278.                         if ( (**hE).caretChPos.v > 0 ) {
  1279.                             charPtr = *(**hE).hText + linePtr[ (**hE).caretChPos.v - 1 ];
  1280.                             while ( *charPtr == TAB || *charPtr == SPACE ) {
  1281.                                 chars[ offset++ ] = *charPtr++;
  1282.                             }
  1283.                         }
  1284. #endif
  1285.                     }
  1286. #ifdef CHAR8_OK
  1287.                     else if ( theChar == TAB || (unsigned char)theChar >= SPACE )
  1288. #else
  1289.                     else if ( theChar == TAB ||                theChar >= SPACE )
  1290. #endif
  1291.                     {
  1292.                         chars[0] = theChar;
  1293.                         offset = 1;
  1294.                     }
  1295.                     else {
  1296.                         break;
  1297.                     }
  1298.                     /* Insert character to right of cursor position */
  1299.                     /*  14Aug92  e  for undo...
  1300.                     eTeEdGuts( hE, chars, (long )offset,
  1301.                                  ( chars[0] == RETURN ? TRUE : FALSE ), TRUE, style );
  1302.                     */
  1303.                     eTeKeyIns( hE, chars[0], style );
  1304.                     /* (**hE).maxRight = (**hE).caretRect.right;
  1305.                        -- done in eTeEdGuts --                   02Oct92  e  */
  1306.                     break;
  1307.             } /* END SWITCH ASCII CHARACTER */
  1308.  
  1309.             /* Update selection flag and turn cursor back on */
  1310.             (**hE).selActive = ( PTL( (**hE).selStart ) != PTL( (**hE).selEnd ) );
  1311.             eTeSetCaretState( hE, CARET_ON );
  1312.             HUnlock( (Handle )(**hE).hLines );
  1313.             break;
  1314.     }
  1315. }
  1316.  
  1317. void eTeClick( eRec **hE, Point hitPt, short modifierKeys, long when )
  1318. {
  1319.     ChPos    pos;
  1320.     Point    newPt;
  1321.     Boolean didScroll = TRUE;
  1322.     long    low, high, offset, start, end, anchorStart, anchorEnd;
  1323.     eRec *pE;
  1324.     
  1325.     HLock( (Handle )hE );
  1326.     pE = *hE;
  1327.  
  1328.     if ( ( hE == gLastViewHit )  &&
  1329.          ( ( when - gLastMouseUp.when ) < GetDblTime() )  &&
  1330.          ( Abs ( gLastMouseDown.where.h - hitPt.h ) < 3 ) &&
  1331.          ( Abs ( gLastMouseDown.where.v - hitPt.v ) < 3 ) )
  1332.         gClicks++;
  1333.     else
  1334.         gClicks = 1;
  1335.     gLastViewHit = hE;
  1336.  
  1337.     eTePrepare( hE );
  1338.  
  1339.     eTeSetCaretState( hE, CARET_OFF );
  1340.  
  1341.     if ( gClicks > 3 )
  1342.         gClicks = 3;
  1343.  
  1344.     if ( modifierKeys & shiftKey ) {
  1345.         gClicks = 1;
  1346.         if ( ! (*pE).selActive ) {
  1347.             (*pE).selStart = (*pE).caretChPos;
  1348.             (*pE).selEnd = (*pE).selStart;
  1349.             (*pE).selActive = TRUE;
  1350.         }
  1351.         start = eTeChPosToOffset( hE, (*pE).selStart );
  1352.         end = eTeChPosToOffset( hE, (*pE).selEnd );
  1353.         /*  24Jul92  e  was...
  1354.         if( modifierKeys & optionKey )
  1355.             anchorStart = anchorEnd = end;
  1356.         else
  1357.             anchorStart = anchorEnd = start;
  1358.         s.b.... */
  1359.         if( eTeChPosToOffset( hE, (*pE).caretChPos ) == start )
  1360.             anchorStart = anchorEnd = (long )( ( modifierKeys & optionKey ) ? start : end );
  1361.         else
  1362.             anchorStart = anchorEnd = (long )( ( modifierKeys & optionKey ) ? end : start );
  1363.         /* since caretChPos is the active end    24Jul92  e  */
  1364.     }
  1365.     else if ( (*pE).selActive ) {
  1366.         eTeHiliteRange( hE, (*pE).selStart, (*pE).selEnd );
  1367.         (*pE).selActive = FALSE;
  1368.     }
  1369.  
  1370.     /*
  1371.      * The DO-WHILE loop monitors the cursor while the mouse button is held down.
  1372.      * The selection is modified with changes in the cursor. NewPt is the new
  1373.      * mouse location, while hitPt contains the last position.
  1374.      */
  1375.     newPt = hitPt;
  1376.     do {
  1377.         if ( didScroll || PTL( newPt ) != PTL( hitPt ) ) {
  1378.             /* first time OR did scroll last loop OR mouse has moved */
  1379.             /* (didScroll == TRUE) causes this to execute first time */
  1380.             switch ( gClicks ) {
  1381.                 case 3:
  1382.                     pos = eTePointToChPos( hE, newPt );
  1383.                     pos.h = 0;
  1384.                     low = eTeChPosToOffset( hE, pos );
  1385.                     if ( pos.v < (*pE).bounds.v - 1 ) {
  1386.                         ++pos.v;
  1387.                         pos.h = 0;
  1388.                     }
  1389.                     else {
  1390.                         pos.h = (*(*pE).hLines)[ pos.v + 1 ] - (*(*pE).hLines)[ pos.v ];
  1391.                     }
  1392.                     high = eTeChPosToOffset( hE, pos );
  1393.                     break;
  1394.  
  1395.                 case 2:
  1396.                     offset = eTeChPosToOffset( hE, eTePointToChPos( hE, newPt ) );
  1397.                     low = WordLimits( *(*pE).hText, offset, TRUE );
  1398.                     high = WordLimits( *(*pE).hText, offset, FALSE );
  1399.                     break;
  1400.  
  1401.                 default:
  1402.                     low = eTeChPosToOffset( hE, eTePointToChPos( hE, newPt ) );
  1403.                     high = low;
  1404.                     break;
  1405.             }
  1406.             /*
  1407.              * Use selActive variable to tell us if this is the first time thru.
  1408.              * If so, then set our anchor points.
  1409.              * Otherwise we just update the selection around the anchors.
  1410.              */
  1411.             if ( ! (*pE).selActive ) {
  1412.                 start = anchorStart = low;
  1413.                 end = anchorEnd = high;
  1414.                 eTeHiliteRange( hE, eTeOffsetToChPos( hE, low ), eTeOffsetToChPos( hE, high ) );
  1415.                 (*pE).selActive = TRUE;
  1416.             }
  1417.             if ( low < start ) {
  1418.                 eTeHiliteRange( hE, eTeOffsetToChPos( hE, low ), eTeOffsetToChPos( hE, start ) );
  1419.                 start = low;
  1420.             }
  1421.             else if ( low < anchorStart ) {
  1422.                 eTeHiliteRange( hE, eTeOffsetToChPos( hE, start ), eTeOffsetToChPos( hE, low ) );
  1423.                 start = low;
  1424.             }
  1425.             else {
  1426.                 eTeHiliteRange( hE, eTeOffsetToChPos( hE, start ), eTeOffsetToChPos( hE, anchorStart ) );
  1427.                 start = anchorStart;
  1428.             }
  1429.             if ( high > end ) {
  1430.                 eTeHiliteRange( hE, eTeOffsetToChPos( hE, end ), eTeOffsetToChPos( hE, high ) );
  1431.                 end = high;
  1432.             }
  1433.             else if ( high > anchorEnd ) {
  1434.                 eTeHiliteRange( hE, eTeOffsetToChPos( hE, high ), eTeOffsetToChPos( hE, end ) );
  1435.                 end = high;
  1436.             }
  1437.             else {
  1438.                 eTeHiliteRange( hE, eTeOffsetToChPos( hE, anchorEnd ), eTeOffsetToChPos( hE, end ) );
  1439.                 end = anchorEnd;
  1440.             }
  1441.             (*pE).selStart = eTeOffsetToChPos( hE, start );
  1442.             (*pE).selEnd = eTeOffsetToChPos( hE, end );
  1443.         }
  1444.         hitPt = newPt;
  1445.         GetMouse( &newPt );
  1446.         didScroll = eTeAutoScroll( hE, newPt );
  1447.     } while ( StillDown() );
  1448.  
  1449.     if ( start == end ) {
  1450.         /*
  1451.         (*pE).caretChPos = eTeOffsetToChPos( hE, start );
  1452.         (*pE).selActive = FALSE;
  1453.         (*pE).selStart = (*pE).caretChPos;
  1454.         (*pE).selEnd = (*pE).caretChPos;
  1455.         */
  1456.         (*pE).selActive = FALSE;
  1457.         (*pE).caretChPos = (*pE).selStart;
  1458.     }
  1459.     else {
  1460.         if( start == anchorStart )
  1461.             (*pE).caretChPos = (*pE).selEnd;
  1462.         else
  1463.             (*pE).caretChPos = (*pE).selStart;
  1464.     }
  1465.     eTeUpdateCaretRect( hE );
  1466.     (*pE).maxRight = (*pE).caretRect.right;
  1467.     eTeSetCaretState( hE, CARET_ON );
  1468.     HUnlock( (Handle )hE );
  1469. }
  1470.  
  1471. /* 2May92  e  -- Style */
  1472.  
  1473. static short eTeOffsetToRun( eRec **hE, long anOffset )
  1474. {
  1475.     register long a;
  1476.     register long *runPtr;
  1477.     register long qRuns, count, j;
  1478.  
  1479.     if ( anOffset < 0 )  return -1;    /* special case for j == 0 below */
  1480.     if ( anOffset == 0 )  return 0;    /* special case for empty text */
  1481.  
  1482.     runPtr = *(**hE).hRuns;
  1483.     count = 0;
  1484.     qRuns = (**hE).qRuns;
  1485.     while ( count < qRuns )    {
  1486.         j = (count + qRuns - 1) >> 1;
  1487.         if( anOffset < runPtr[ j ] )
  1488.             qRuns = j;
  1489.         else if( anOffset >= runPtr[ j + 1 ] )
  1490.             count = j + 1;
  1491.         else    {
  1492.             count = j;
  1493.             break;
  1494.         }
  1495.     }
  1496.     return count;
  1497. }
  1498.  
  1499. static short eTeRunToStyle( eRec **hE, short r )
  1500. {
  1501.     return ( ( r & 1 ) ^ (**hE).fustStyle );
  1502. }
  1503.  
  1504. /* 12Sep92  e  */
  1505. void eTeGetRun( eRec **hE, long *sta, long *end )
  1506. {
  1507.     long car = eTeChPosToOffset( hE, (**hE).caretChPos );
  1508.     short run = eTeOffsetToRun( hE, car-1 );
  1509.     short sty = eTeRunToStyle( hE, run );
  1510.     if( run < 0 || sty == 0 )
  1511.       *sta = *end = car;
  1512.     else
  1513.     { long *runVec = *(**hE).hRuns;
  1514.       *sta = runVec[run];
  1515.       *end = runVec[run+1];
  1516.     }
  1517. }
  1518.  
  1519. static void eTeUpdateRuns( eRec **hE, long j, long k, long n, short ns )
  1520. {    /* removing chars j..k, inserting n chars of style ns */
  1521.     short jr = eTeOffsetToRun( hE, j - 1 );
  1522.     short kr = eTeOffsetToRun( hE, k );
  1523.     short js = eTeRunToStyle( hE, jr );
  1524.     short ks = eTeRunToStyle( hE, kr );
  1525.     long adj = n - ( k - j );
  1526.     long *runVec = *(**hE).hRuns;
  1527.     short qRuns = (**hE).qRuns;
  1528.     
  1529.     if( n == 0 ) ns = ks;
  1530.     
  1531.     if( ( ns != js ) || ( j == 0 ) )
  1532.     {    /* can't merge n with j */
  1533.         if( j == 0 ) (**hE).fustStyle = ns;
  1534.         /* 10Aug92  e  was: 
  1535.         if( (( n != 0 ) && ( kr == qRuns )) || (( ns != ks ) && ( kr <= jr + 1 )) ) */
  1536.         if( ( n != 0 ) && ( kr <= jr + 1 ) && ( kr == qRuns || ns != ks ) )
  1537.         {    /* grow */
  1538.             short indx, incr = ( ( j == 0 ) || ( kr == qRuns ) ) ? 1 : 2;
  1539.             qRuns += incr;
  1540.             if ( qRuns >= (**hE).runsAllocated )
  1541.             {    (**hE).runsAllocated += MIN_RUNS;
  1542.                 SetHandleSize( (Handle )(**hE).hRuns, (**hE).runsAllocated * sizeof( long ) );
  1543.                 runVec = *(**hE).hRuns;
  1544.             }
  1545.             kr = jr + incr;
  1546.             for( indx = qRuns; indx > kr; indx-- )
  1547.                 runVec[indx] = runVec[indx-incr] + adj;
  1548.             runVec[++jr] = j;
  1549.             if( jr != kr ) runVec[++jr] = j + n;
  1550.             (**hE).qRuns = qRuns;
  1551.             return;
  1552.         }
  1553.         runVec[++jr] = j;
  1554.     }
  1555. #ifdef use_old_buggy_code
  1556.     /* 16Sep92  e  was... */
  1557.     else if( ( kr == qRuns ) && ( ns == ks ) /* && ( ns == js ) */ )
  1558.         kr--;
  1559.     if( ns == ks )
  1560. #endif
  1561.     if( ns == ks && ( kr != qRuns || ns != js ) )    /* 16Sep92  e */
  1562.     {    /* merge k with n (1&3) */
  1563.         while( kr < qRuns ) runVec[++jr] = runVec[++kr] + adj;
  1564.     }
  1565.     else
  1566.     {    /* can't merge k with n (2&4) */
  1567.         if( k > runVec[kr] ) { runVec[++jr] = j + n; kr++; }    /*  13Oct92  e  */
  1568.         while( kr <= qRuns ) runVec[++jr] = runVec[kr++] + adj;
  1569.     }
  1570.     (**hE).qRuns = jr;
  1571.     /* see if we should shrink the runs vector */
  1572.     if ( jr + MIN_RUNS + MIN_RUNS < (**hE).runsAllocated )
  1573.     {    (**hE).runsAllocated = ( jr / MIN_RUNS + 1 ) * MIN_RUNS;
  1574.         SetHandleSize( (Handle )(**hE).hRuns, (**hE).runsAllocated * sizeof( long ) );
  1575.     }
  1576. }
  1577.  
  1578. /* ******** */
  1579.  
  1580. static short eTeUpdateLineStarts( eRec **hE, short firstLine )
  1581. {
  1582.     char hdlState;
  1583.     short error = noErr;
  1584.     register LineRec     *linePtr;
  1585.     register char        *charPtr, c;
  1586.     register short        numLines;
  1587.     register long        maxLine;    /* 28May92  e */
  1588.     register long        offsLine;    /* 28May92  e */
  1589.     register short        wrap;        /*  5Jul92  e */
  1590.     eRec    *pE;
  1591.     char    *pT;
  1592.  
  1593.     MoveHHi( (Handle )hE );
  1594.     HLock( (Handle )hE );
  1595.     pE = *hE;
  1596.     MoveHHi( (*pE).hText );
  1597.     HLock( (*pE).hText );
  1598.     pT  = *(*pE).hText;
  1599.     HLock( (Handle )(*pE).hLines );
  1600.  
  1601.     linePtr = *(*pE).hLines;
  1602.     numLines = firstLine + 1;
  1603.     
  1604.     maxLine = (firstLine == 0) ? 0 : (*pE).bounds.h;    /* 28May92  e  */
  1605.     wrap = (*pE).wrap;                                    /*  5Jul92  e  */
  1606.  
  1607.     /* Scan entire block of text, start with line firstLine */
  1608.     for ( charPtr = linePtr[ firstLine ] + pT; ( c = *charPtr ) != '\0';  ++charPtr )
  1609.     {    /* Found end of line. Store next line's starting position */
  1610.         if ( c == RETURN
  1611.              || ( --wrap <= 0 && (c = charPtr[1]) != RETURN && c != '\0' ) )
  1612.              /* 10Aug92  e  added RETURN test  |  11Aug92  e  added '\0' test  */
  1613.         {    wrap = (*pE).wrap;                                    /*  5Jul92  e  */
  1614.             offsLine = charPtr - pT + 1;
  1615.             if( ( offsLine - linePtr[firstLine] ) >= maxLine )    /* 10Aug92  e  was > */
  1616.                 maxLine = offsLine - linePtr[firstLine] + 1;    /* 10Aug92  e  +1 */
  1617.             linePtr[ ++firstLine ] = offsLine;
  1618.             /* */
  1619.             ++numLines;
  1620.             /* See if we need to allocate more space for our line starts */
  1621.             if ( numLines >= (*pE).linesAllocated )
  1622.             {    (*pE).linesAllocated += MIN_LINES;
  1623.                 HUnlock( (Handle )(*pE).hLines );
  1624.                 SetHandleSize( (Handle )(*pE).hLines, (*pE).linesAllocated * sizeof( LineRec ) );
  1625.                   asm { move.w D0, error }         /* error = MemError(); */
  1626.                   if( error != noErr )
  1627.                   {    HUnlock( (*pE).hText );
  1628.                       HUnlock( (Handle )hE );
  1629.                       return error;
  1630.                   }
  1631.                 MoveHHi( (Handle )(*pE).hLines );
  1632.                 HLock( (Handle )(*pE).hLines );
  1633.                 linePtr = *(*pE).hLines;
  1634.             }
  1635.         }
  1636.     }
  1637.     /* Last entry contains length of entire text block */
  1638.     offsLine = charPtr - pT;
  1639.     if( ( offsLine - linePtr[firstLine] ) >= maxLine )            /* 10Aug92  e  was > */
  1640.         maxLine = offsLine - linePtr[firstLine] + 1;            /* 10Aug92  e  +1 */
  1641.     linePtr[ firstLine + 1 ] = offsLine;
  1642.  
  1643.     HUnlock( (*pE).hText );
  1644.     HUnlock( (Handle )(*pE).hLines );
  1645.  
  1646.     /* See if we should shrink our line starts block */
  1647.     if ( numLines + MIN_LINES < (*pE).linesAllocated ) {
  1648.         (*pE).linesAllocated = ( numLines / MIN_LINES + 1 ) * MIN_LINES;
  1649.         SetHandleSize( (Handle )(*pE).hLines, (*pE).linesAllocated * sizeof( LineRec ) );
  1650.           asm { move.w D0, error }         /* error = MemError(); */
  1651.     }
  1652.  
  1653.     (*pE).bounds.h = Min( maxLine, 32767 );  /* 28May92  e  */
  1654.     (*pE).bounds.v = numLines;
  1655.     HUnlock( (Handle )hE );
  1656.     eTeAdjustScrollMax( hE );
  1657.     return error;
  1658. }
  1659.  
  1660. static short eTeSetTextGuts( eRec** hE, Handle hT, long numChars )
  1661. {    short error = noErr;
  1662.     if ( (**hE).hText )
  1663.         DisposHandle        DisposHaScrollMax( hE */
  1664.     line hE  10A-1 ] != 0 ) {
  1665.         e hE  10A+= 1;
  1666.         SetHandleSize( hT, numChars );
  1667.           asm { move.w D0, error }         /* error = MemError(); */
  1668.           if( error != noErr ) return error;
  1669.     }
  1670.     (*
  1671.     line hE  10A- 1 ] = '\0';
  1672.     (**hE).hText = hT;
  1673.     error = eTeUpdateLineStarts( hE, 0 );
  1674.       if( error != noErr ) return error;
  1675.     error = eTeNewRuns( hE, n hE  10A- 1 );
  1676.       if( error != noErr ) return error;
  1677.     (**hE).caretChPos.h = (**hE).caretChPos.v = 0L;
  1678.     (**hE).selStart = (**hE).selEnd = (**hE).caretChPos;
  1679.     (**hE).maxRight = 0;
  1680.     eTeUpdateCaretRect( hE );
  1681.     (**hE).dirty = FALSE;
  1682.     eTeRefresh( hE );                            /* 17Dec92  e  */
  1683.     return error;
  1684. }
  1685.  
  1686.  
  1687. short eTeSetTextHandleDetabify( eRec **hE, Handle hT, short tabstops )
  1688. {    long count;
  1689.     long size = GetHandleSize( hT );
  1690.     short error = DeTabifyHandle        hT, &size, &count, tabstops >= 0 ? tabstops : (**hE).tabStops )Max( hE error != noErr ) return error;
  1691.     /* see if count lines fit in lineStarts array */
  1692.     if( count >hE 32767 - ( MIN_LINES >> 1) ) )
  1693.         return -357Max( hE count >= (**hE).linesAllocated )
  1694.     {    (**hE).linesAllocated = count + ( MIN_LINES >> 1);
  1695.         SetHandleSize( (Handle )(**hE).hLines, (**hE).linesAllocated * sizeof( LineRec ) );
  1696.           asm { move.w D0, error }         /* error = MemError(); */
  1697.           if( error != noErr )
  1698.           {    (**hE).linesAllocated = 0;
  1699.               return error;
  1700.           }
  1701.     }
  1702.     return eTeSetTextGuts( hE, hT, size );
  1703. }
  1704.  
  1705. short eTeSetTextHandle( eRec** hE, Handle hT )
  1706. {
  1707.     return eTeSetTextGuts( hE, hT, GetHandleSize( hT ) );
  1708. }
  1709.  
  1710. short eTeSetTextPtr( eRec** hE, Ptr pT, long numChars )
  1711. {
  1712.     Handle hT;
  1713.     
  1714.     if ( pTine hE  10A-1 ] != 0 )
  1715.         e hE  10A+= 1;
  1716.     PtrToHand( pT, &hT, numChars );
  1717.     (*
  1718.     line hE  10A- 1 ] = '\0';
  1719.     return eTeSetTextGuts( hE, hT, numChars );
  1720. }
  1721.  
  1722. void eTeSetSelect( eRec **hE, long aStart, long anEnd )
  1723. {
  1724.     ChPos    first, last;
  1725.     /* Unhilite existing selection */
  1726.     if ( (**hE).selActive ) {
  1727.         first = (**hE).selStart;
  1728.         last = (**hE).selEnd;
  1729.         eTeHiliteRange( hE, first, last );
  1730.         (**hE).selActive = FALSE;
  1731.     }
  1732.     else
  1733.         eTeSetCaretState( hE, CARET_OFF );
  1734.  
  1735.     if ( aStart < 0 ) aStart = 0;
  1736.     if ( anEnd < 0 ) anEnd = 0;
  1737.     if ( aStart >= anEnd ) {
  1738.         (**hE).selStart = (**hE).selEnd = eTeOffsetToChPos( hE, aStart );
  1739.     }
  1740.     else {
  1741.         (**hE).selActive = TRUE;
  1742.         first = eTeOffsetToChPos( hE, aStart );
  1743.         last = eTeOffsetToChPos( hE, anEnd );
  1744.         (**hE).selStart = first;
  1745.         (**hE).selEnd = last;
  1746.         eTeHiliteRange( hE, first, last );
  1747.     }
  1748.     (**hE).caretChPos = (**hE).selEnd;
  1749.     eTeUpdateCaretRect( hE );
  1750.     (**hE).maxRight = (**hE).caretRect.right;
  1751. }
  1752.  
  1753. static Boolean eTeHasReturns( register char *data, register long len )
  1754. {
  1755.     while( len-- )
  1756.         if ( *data++ == RETURN )
  1757.             return TRUE;
  1758.     return FALSE;
  1759. }
  1760.  
  1761. /* the scrap  13Aug92  e  */
  1762.  
  1763. void     eTePutScrap()
  1764. {    OSErr        err;
  1765.  
  1766.     HLock( eTeScrap );
  1767.     /* Make a copy of the scrap and give to the clipboard */
  1768.     if( ( err = ZeroScrap() ) == noErr )
  1769.     {    err = PutScrap( eTeScrapLen, 'TEXT', *eTeScrap );
  1770.     }
  1771.     if( err == noErr )
  1772.     {    
  1773.     }
  1774.     else SysBeep( 3 );
  1775.     eTeScrapCnt = (InfoScrap())->scrapCount;
  1776.     HUnlock( eTeScrap );
  1777. }
  1778.  
  1779. static void puntUndoStuff()
  1780. {
  1781.     eTeUndoStuff.undoTeRec = NULL;
  1782.     eTeUndoStuff.undoTitle = utUndo;
  1783.     eTeUndoStuff.undoProc  = undoBugNi;
  1784. }
  1785.  
  1786. void     eTeGetScrap()
  1787. {    long offset, length;
  1788.  
  1789.     if( eTeScrapCnt != (InfoScrap())->scrapCount )
  1790.     {    length = GetScrap( NULL, 'TEXT', &offset );
  1791.         if ( length >= 0 )
  1792.         { eTeScrapLen = GetScrap( eTeScrap, 'TEXT', &offset );
  1793.           puntUndoStuff();
  1794.         }
  1795.         else if( length != noTypeErr )
  1796.         { SysBeep( 3 );
  1797.           SetHandleSize( eTeScrap, 0 );
  1798.           eTeScrapLen = 0;
  1799.           puntUndoStuff();
  1800.         }
  1801.         /* else punt: noTypeErr => nothing to copy */
  1802.     }
  1803. }
  1804.  
  1805. /* I don't feel bad using a large buffer on the stack,
  1806.    even on a MacPlus Quickdraw uses several K of stack space to draw text! */
  1807. #define TR_BUF_SZ 4000
  1808.  
  1809. /* ####################################################################
  1810.  
  1811. 14Aug92  e
  1812.  
  1813. Undo notes...
  1814.  
  1815. try to alloc a temp handle in dodoTyping()
  1816.  
  1817. concatenate successive kills (use clip or undo ?) ?
  1818.  
  1819. add to eTeKeyIns() mechanism for delete and del>
  1820.  
  1821.   if undoTyping or undoClear already active && there's no selection
  1822.  
  1823.     if normal char
  1824.       if caret >= undoStart && caret <= undoStart + insertLen
  1825.       increment insertLen
  1826.       insert char
  1827.     else
  1828.       start new undo & keyAccum
  1829.  
  1830.     if Delete
  1831.       if caret > undoStart && caret <= undoStart + insertLen
  1832.         decr insertLen
  1833.         do the delete
  1834.       else if caret == undoStart
  1835.         put character at caret-1 onto front of undoText
  1836.         do the delete
  1837.       else
  1838.         start new undo & keyAccum
  1839.  
  1840.     if Del>
  1841.       if caret >= undoStart && caret < undoStart + insertLen
  1842.         decr insertLen
  1843.         do the delete
  1844.       else if caret == undoStart + insertLen
  1845.         put character at caret onto end of undoText
  1846.         do the delete
  1847.       else
  1848.         start new undo & keyAccum
  1849.  
  1850.   else
  1851.     what's done now
  1852.  
  1853. ensureSelOK() is only necessary
  1854.  because caretChPos is not kept syncronized with selStart & selEnd and vice versa
  1855.  should this be fixed so ensureSelOK() can be eliminated?  for other reasons?
  1856.  
  1857. make eTeTranspose() work better with undo -- at least adjust offsets?
  1858.  
  1859. subroutinize undo routines
  1860.   -- eTeEdGuts( hE, EMPTY_PTR, 0L, FALSE, TRUE, 0 ); ??
  1861.   -- hoseUndoText();
  1862.   -- rsSelectionPts();
  1863.  
  1864. /* ##################### */
  1865.  
  1866. static void ensureSelOK( eRec **hE )                /* can this be eliminated !? */
  1867. {    if( ! (**hE).selActive )
  1868.         (**hE).selStart = (**hE).selEnd = (**hE).caretChPos;
  1869. }
  1870.  
  1871. void eTeUndo( eRec **hE )
  1872. {
  1873.     if( hE == eTeUndoStuff.undoTeRec )
  1874.         eTeUndoStuff.undoProc( hE );
  1875.     else SysBeep( 3 );
  1876. }
  1877.  
  1878. void eTeEditMenuUpdate( eRec **hE, MenuHandle hM )
  1879. {
  1880.     SetItem( hM, 1, eTeUndoStuff.undoTitle );        /* memoize this ?! */
  1881.     if( hE != NULL )
  1882.     {    if( (**hE).selActive )
  1883.         {    EnableItem( hM, 3 ); /* cut */
  1884.             EnableItem( hM, 4 ); /* copy */
  1885.         }
  1886.         else
  1887.         {    DisableItem( hM, 3 ); /* cut */
  1888.             DisableItem( hM, 4 ); /* copy */
  1889.         }
  1890.         EnableItem( hM, 5 ); /* paste */
  1891.         EnableItem( hM, 6 ); /* clear */
  1892.         if( hE == eTeUndoStuff.undoTeRec )
  1893.             EnableItem( hM, 1 );
  1894.         else
  1895.             DisableItem( hM, 1 ); /* undo */
  1896.     }
  1897.     else
  1898.     {    EnableItem( hM, 3 ); /* cut */
  1899.         EnableItem( hM, 4 ); /* copy */
  1900.         EnableItem( hM, 5 ); /* paste */
  1901.         EnableItem( hM, 6 ); /* clear */
  1902.         DisableItem( hM, 1 ); /* undo */
  1903.     }
  1904. }
  1905.  
  1906. static void ssSelectionPts( eRec **hE )
  1907. {    ensureSelOK( hE );
  1908.     eTeUndoStuff.undoInLen = 0;
  1909.     eTeUndoStuff.undoKeyAccum = FALSE;
  1910.     eTeUndoStuff.undoStart = eTeChPosToOffset( hE, (**hE).selStart );
  1911.     eTeUndoStuff.undoEnd   = eTeChPosToOffset( hE, (**hE).selEnd   );
  1912.     eTeUndoStuff.undoCarStaP = PTL( (**hE).caretChPos ) == PTL( (**hE).selStart );
  1913.     eTeUndoStuff.undoDirty = (**hE).dirty;
  1914.     eTeUndoStuff.undoTeRec = hE;
  1915. }
  1916.  
  1917. static void ssSwapScrap()
  1918. {    Handle h;
  1919.     long l;
  1920.     h = eTeScrap;
  1921.     l = eTeScrapLen;
  1922.     eTeScrap = eTeUndoStuff.undoText;
  1923.     eTeScrapLen = eTeUndoStuff.undoTxLen;
  1924.     eTeUndoStuff.undoText = h;
  1925.     eTeUndoStuff.undoTxLen = l;
  1926. }
  1927.  
  1928. static void eTeSetSelCar( eRec **hE, long aStart, long anEnd, Boolean stap )
  1929. {
  1930.     eTeSetSelect( hE, aStart, anEnd );
  1931.     if( stap ) (**hE).caretChPos = (**hE).selStart;
  1932. }
  1933.  
  1934. static void eTePasteNu( eRec **hE, short style )
  1935. {
  1936.     MoveHHi( eTeScrap );
  1937.     HLock( eTeScrap );
  1938.     eTeEdGuts( hE, *eTeScrap, eTeScrapLen, eTeHasReturns( *eTeScrap, eTeScrapLen ), TRUE, style );
  1939.     HUnlock( eTeScrap );
  1940. }
  1941.  
  1942. static void eTeCopyNu( eRec **hE )
  1943. {
  1944.     long        length, offset;
  1945.  
  1946.     /* Get starting position and length of text to copy */
  1947.     offset = eTeChPosToOffset( hE, (**hE).selStart );
  1948.     length = eTeChPosToOffset( hE, (**hE).selEnd )A- offset;
  1949.     MoveHHi( DisposHaScrollMaxHLock( DisposHaScrollMax/* Make a copy of the text and put in scrap */
  1950.     offset = Munger( eTeScrap, 0L, NULL, eTeScrapLen, *DisposHaScrol+ offset, length );
  1951.     if( offset == length )
  1952.         eTeScrapLen = length; /* success */
  1953.     else SysBeep( 3 );
  1954.     HUnlock( (isposHaScrollMa}
  1955.  
  1956. static void undoCut( eRec  **hE )                /* Undo Cut */
  1957. {                                                /* restore selection from scrap */
  1958.     eTeSetSelect( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoStart );
  1959.     eTePasteNu( hE, 0 );
  1960.     ssSwapScrap();                                 /* restore scrap */
  1961.     SetHandleSize( eTeUndoStuff.undoText, 0 );
  1962.     eTeUndoStuff.undoTxLen = 0;
  1963.     eTeSetSelCar( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoEnd, eTeUndoStuff.undoCarStaP );
  1964.     (**hE).dirty = eTeUndoStuff.undoDirty;        /* restore selection points & flags */
  1965.     eTeUndoStuff.undoTitle = rtCut;
  1966.     eTeUndoStuff.undoProc  = eTeCut;
  1967. }
  1968.   
  1969. void eTeCut( eRec **hE )                        /*  Cut == RedoCut  */
  1970. {    if( ! (**hE).selActive ) return;
  1971.     ssSelectionPts( hE );  /* snapshot selection points */
  1972.     ssSwapScrap();         /* snapshot scrap */
  1973.     eTeUndoStuff.undoTitle = utCut;
  1974.     eTeUndoStuff.undoProc  = undoCut;
  1975.     eTeCopyNu( hE );                            /* do cut */
  1976.     eTeEdGuts( hE, EMPTY_PTR, 0L, FALSE, TRUE, 0 );a}
  1977.  
  1978. static void undoCopy( eRec  **hE )                /* Undo Copy */
  1979. {    
  1980.     ssSwapScrap();                                 /* restore scrap */
  1981.     SetHandleSize( eTeUndoStuff.undoText, 0 );
  1982.     eTeUndoStuff.undoTxLen = 0;
  1983.     eTeSetSelCar( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoEnd, eTeUndoStuff.undoCarStaP );
  1984.     (**hE).dirty = eTeUndoStuff.undoDirty;        /* restore selection points & flags */
  1985.     eTeUndoStuff.undoTitle = rtCopy;
  1986.     eTeUndoStuff.undoProc  = eTeCopy;
  1987. }
  1988.   
  1989. void eTeCopy( eRec **hE )                        /* Copy == Redo Copy */
  1990. {    if( ! (**hE).selActive ) return;
  1991.     ssSelectionPts( hE );                          /* snapshot selection points */
  1992.     ssSwapScrap();                                 /* snapshot scrap */
  1993.     eTeUndoStuff.undoTitle = utCopy;
  1994.     eTeUndoStuff.undoProc  = undoCopy;
  1995.     eTeCopyNu( hE );                            /* do copy */
  1996. }
  1997.  
  1998. static void redoPaste( eRec  **hE )                /* Redo Paste */
  1999. {    eTePaste( hE, 0 );
  2000. }
  2001.  
  2002. static void undoPaste( eRec  **hE )                /* Undo Paste */
  2003. {                                                /* restore selection from scrap */
  2004.     eTeSetSelect( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoStart + eTeScrapLen );
  2005.     ssSwapScrap();                                 /* swap scrap & undo */
  2006.     eTePasteNu( hE, 0 );                        /* restore text from undo */
  2007.     ssSwapScrap();                                 /* swap scrap & undo */
  2008.     SetHandleSize( eTeUndoStuff.undoText, 0 );
  2009.     eTeUndoStuff.undoTxLen = 0;
  2010.     eTeSetSelCar( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoEnd, eTeUndoStuff.undoCarStaP );
  2011.     (**hE).dirty = eTeUndoStuff.undoDirty;        /* restore selection points & flags */
  2012.     eTeUndoStuff.undoTitle = rtPaste;
  2013.     eTeUndoStuff.undoProc  = redoPaste;
  2014. }
  2015.  
  2016. void eTePaste( eRec **hE, short style )            /* Paste != Redo Paste */
  2017. {    ssSelectionPts( hE );                          /* snapshot selection points */
  2018.     ssSwapScrap();                                 /* snapshot selection */
  2019.     eTeCopyNu( hE );
  2020.     ssSwapScrap();                                 /* restore scrap */
  2021.     eTeUndoStuff.undoTitle = utPaste;
  2022.     eTeUndoStuff.undoProc  = undoPaste;
  2023.     eTePasteNu( hE, style );                    /* do paste [14Sep92  e  -- added style back] */
  2024. }
  2025.  
  2026. static void undoDelete( eRec **hE )                /* Undo Clear */
  2027. {                                                /* restore selection from scrap */
  2028.     eTeSetSelect( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoStart );
  2029.     ssSwapScrap();                                 /* swap scrap & undo */
  2030.     eTePasteNu( hE, 0 );                        /* restore text from undo */
  2031.     ssSwapScrap();                                 /* swap scrap & undo */
  2032.     SetHandleSize( eTeUndoStuff.undoText, 0 );
  2033.     eTeUndoStuff.undoTxLen = 0;
  2034.     eTeSetSelCar( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoEnd, eTeUndoStuff.undoCarStaP );
  2035.     (**hE).dirty = eTeUndoStuff.undoDirty;        /* restore selection points & flags */
  2036.     eTeUndoStuff.undoTitle = rtClear;
  2037.     eTeUndoStuff.undoProc  = eTeDelete;
  2038. }
  2039.  
  2040. void eTeDelete( eRec **hE )                        /* Clear == Redo Clear */
  2041. {    long offset;
  2042.     if ( ! (**hE).selActive )
  2043.     {    /* Just delete previous character */
  2044.         offset = eTeChPosToOffset( hE, (**hE).caretChPos );
  2045.         if ( offset <= 0 ) return;
  2046.         ssSelectionPts( hE );                          /* snapshot selection points */
  2047.         eTeUndoStuff.undoStart--;
  2048.         (**hE).selEnd = (**hE).caretChPos;
  2049.         (**hE).selStart = eTeOffsetToChPos( hE, --offset );
  2050.     }
  2051.     else
  2052.         ssSelectionPts( hE );                          /* snapshot selection points */
  2053.     ssSwapScrap();                                 /* snapshot selection */
  2054.     eTeCopyNu( hE );
  2055.     ssSwapScrap();                                 /* restore scrap */
  2056.     eTeUndoStuff.undoTitle = utClear;
  2057.     eTeUndoStuff.undoProc  = undoDelete;
  2058.     if ( (**hE).selActive )
  2059.         eTeEdGuts( hE, EMPTY_PTR, 0L, FALSE, TRUE, 0 );a    else
  2060.     {    (**hE).selEnd = (**hE).caretChPos = (**hE).selStart;
  2061.         eTeSetCaretState( hE, CARET_OFF ); /* 23Jul92  e */
  2062.         eTeUpdateCaretRect( hE );
  2063.         eTeEdGuts( hE, EMPTY_PTR, -1L, *DiDisposHaScrol+ offset) == RETURN, TRUE, 0 );a    }
  2064. }
  2065.  
  2066. static void redoKill( eRec **hE )                /* Redo Kill */
  2067. {
  2068.     eTeSetSelect( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoStart );
  2069.     eTeKillTo( hE, eTeOffsetToChPos( hE, eTeUndoStuff.undoEnd ) );
  2070. }
  2071.  
  2072. static void undoKill( eRec  **hE )                /* Undo Kill */
  2073. {                                                /* restore selection from scrap */
  2074.     eTeSetSelect( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoStart );
  2075.     eTePasteNu( hE, 0 );
  2076.     ssSwapScrap();                                 /* restore scrap */
  2077.     SetHandleSize( eTeUndoStuff.undoText, 0 );
  2078.     eTeUndoStuff.undoTxLen = 0;
  2079.     eTeSetSelect( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoStart );
  2080.     (**hE).dirty = eTeUndoStuff.undoDirty;        /* restore selection points & flags */
  2081.     eTeUndoStuff.undoTitle = rtKill;
  2082.     eTeUndoStuff.undoProc  = redoKill;
  2083. }
  2084.   
  2085. void eTeKillTo( eRec **hE, ChPos chPos )        /* Kill != Redo Kill */
  2086. {
  2087.     if( PTL( chPos ) == PTL( (**hE).caretChPos ) ) return;
  2088.     (**hE).selStart = (**hE).caretChPos;        /* mung selection points first! */
  2089.     (**hE).selEnd = chPos;
  2090.     (**hE).selActive = TRUE;
  2091.     ssSelectionPts( hE );                          /* snapshot selection points */
  2092.     ssSwapScrap();                                 /* snapshot scrap */
  2093.     eTeCopyNu( hE );
  2094.     eTeUndoStuff.undoTitle = utKill;
  2095.     eTeUndoStuff.undoProc  = undoKillMax/* since we'll redraw the line anyway, eTeHiliteRange isn't necessary!?    */
  2096.     /*  unfortunately, for wrapped lines, it isA- otherwise cursor turds; ugh. */
  2097.     /* eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );                   */
  2098.     /* instead we'll fake it out by making the window look inactive instead    */
  2099.     (**hE).active = FALSE;
  2100.     eTeEdGuts( hE, EMPTY_PTR, 0L, FALSE, TRUE, 0 );a    (**hE).active = TRUE;
  2101. }
  2102.  
  2103. static Boolean dodoTyping( eRec **hE )
  2104. {    char buf[TR_BUF_SZ];
  2105.     long sz;
  2106.     register char *p, *q;
  2107.     register long iter = sz = eTeUndoStuff.undoTxLen;;
  2108.     
  2109.     if( iter > 0 || eTeUndoStuff.undoInLen > 0 )
  2110.     {
  2111.         if( eTeUndoStuff.undoInLen > TR_BUF_SZ )
  2112.         {    SysBeep( 6 );                            /* try to alloc a temp handle ? */
  2113.             return FALSE;
  2114.         }
  2115.         q = buf;
  2116.         p = *eTeUndoStuff.undoText;
  2117.         while( iter-- ) *q++ = *p++;
  2118.         eTeSetSelect( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoStart + eTeUndoStuff.undoInLen );
  2119.         ssSwapScrap();                                 /* snapshot insertion */
  2120.         eTeCopyNu( hE );
  2121.         ssSwapScrap();                                 /* restore scrap */
  2122.         eTeEdGuts( hE, buf, sz, eTeHasReturns( buf, sz ), TRUE, 0 );
  2123.         eTeUndoStuff.undoInLen = sz;
  2124.     }
  2125.     return TRUE;
  2126. }
  2127.  
  2128. static void redoTyping( eRec **hE );
  2129.  
  2130. static void undoTyping( eRec **hE )
  2131. {    if( dodoTyping( hE ) )
  2132.     {    eTeUndoStuff.undoKeyAccum = FALSE;
  2133.         eTeSetSelCar( hE, eTeUndoStuff.undoStart, eTeUndoStuff.undoEnd, eTeUndoStuff.undoCarStaP );
  2134.         (**hE).dirty = eTeUndoStuff.undoDirty;        /* restore selection points & flags */
  2135.         eTeUndoStuff.undoTitle = rtTyping;
  2136.         eTeUndoStuff.undoProc  = redoTyping;
  2137.     }
  2138. }
  2139.     
  2140. static void redoTyping( eRec **hE )
  2141. {    eTeUndoStuff.undoKeyAccum = TRUE;
  2142.     eTeUndoStuff.undoDirty = (**hE).dirty;
  2143.     if( dodoTyping( hE ) )
  2144.     {    eTeUndoStuff.undoTitle = utTyping;
  2145.         eTeUndoStuff.undoProc  = undoTyping;
  2146.     }
  2147.     else
  2148.     {    eTeUndoStuff.undoKeyAccum = FALSE;
  2149.     }
  2150. }
  2151.  
  2152. static void eTeKeyIns( eRec **hE, char c, short style )
  2153. {
  2154.     if( eTeUndoStuff.undoKeyAccum
  2155.         && hE == eTeUndoStuff.undoTeRec
  2156.         && (**hE).selActive == FALSE
  2157.         && eTeChPosToOffset( hE, (**hE).caretChPos )
  2158.              == eTeUndoStuff.undoStart + eTeUndoStuff.undoInLen
  2159.       )
  2160.     {    eTeUndoStuff.undoInLen += 1;            /* incr numChars inserted */
  2161.     }
  2162.     else
  2163.     {    ssSelectionPts( hE );                      /* snapshot selection points */
  2164.         eTeUndoStuff.undoKeyAccum = TRUE;
  2165.         ssSwapScrap();                             /* snapshot selection */
  2166.         eTeCopyNu( hE );
  2167.         ssSwapScrap();                             /* restore scrap */
  2168.         eTeUndoStuff.undoInLen = 1;                /* remember numChars inserted */
  2169.         eTeUndoStuff.undoTitle = utTyping;
  2170.         eTeUndoStuff.undoProc  = undoTyping;
  2171.     }
  2172.     eTeEdGuts( hE, &c, 1, c == RETURN, TRUE, style );
  2173. }
  2174.  
  2175. void eTeInsert( eRec **hE, Ptr textPtr, long numChars, short style )
  2176. {    if( numChars <= 0 ) { eTeDelete( hE ); return; } /* 08Jan93  e  */
  2177.     ssSelectionPts( hE );                          /* snapshot selection points */
  2178.     eTeUndoStuff.undoKeyAccum = TRUE;
  2179.     ssSwapScrap();                                 /* snapshot selection */
  2180.     eTeCopyNu( hE );
  2181.     ssSwapScrap();                                 /* restore scrap */
  2182.     eTeUndoStuff.undoInLen = numChars;            /* remember numChars inserted */
  2183.     eTeUndoStuff.undoTitle = utInsert;
  2184.     eTeUndoStuff.undoProc  = undoTyping;
  2185.     eTeEdGuts( hE, textPtr, numChars, eTeHasReturns( textPtr, numChars ), TRUE, style );
  2186. }
  2187.  
  2188. /* #################################################################### */
  2189.  
  2190. /* eTeTranspose  22Jul92  e  */
  2191.  
  2192. static void eTeTransposeGuts1( eRec **hE, long beg, long mid, long end )
  2193. { char buf[TR_BUF_SZ];
  2194.   long shift = midA- beg;
  2195.   register char *p, *q;
  2196.   register long incr, iter;
  2197.   
  2198.   while( shift > 0 )
  2199.   { incr = Min( shift, TR_BUF_SZ );
  2200.     shift -= incr;
  2201.     /* */
  2202.     p = *DisposHaScrol+ beg;
  2203.     q = buf;
  2204.     iter = incr;
  2205.     while( iter-- ) *q++ = *p++;
  2206.     /* */
  2207.     p = *DisposHaScrol+ begl+ incr;
  2208.     q = *DisposHaScrol+ beg;
  2209.     iter = end - beg - incr;
  2210.     while( iter-- ) *q++ = *p++;
  2211.     /* */
  2212.     p = buf;
  2213.     q = *DisposHaScrol+ end - incr;
  2214.     iter = incr;
  2215.     while( iter-- ) *q++ = *p++;
  2216.   }
  2217. }
  2218.  
  2219. static void eTeTransposeGuts0( eRec **hE, long beg, long midl, long midr, long end )
  2220. { char buf[TR_BUF_SZ];
  2221.   register char *p, *q;
  2222.   register long iter;
  2223.   
  2224.   q = buf;
  2225.   p = *DisposHaScrol+ midl;
  2226.   iter = midr - midl;
  2227.   while( iter-- ) *q++ = *p++;
  2228.   p = *DisposHaScrol+ beg;
  2229.   iter = midlA- beg;
  2230.   while( iter-- ) *q++ = *p++;
  2231.   q = *DisposHaScrol+ beg;
  2232.   p = *DisposHaScrol+ midr;
  2233.   iter = end - midr;
  2234.   while( iter-- ) *q++ = *p++;
  2235.   p = buf;
  2236.   iter = midr - beg;
  2237.   while( iter-- ) *q++ = *p++;
  2238. }
  2239.  
  2240. static void eTeTransposeGuts2( eRec **hE, long beg, long midl, long midr, long end )
  2241.   if( midr - beg <= TR_BUF_SZ )
  2242.   { eTeTransposeGuts0( hE, beg, midl, midr, end );
  2243.   }
  2244.   else
  2245.   { eTeTransposeGuts1( hE, beg, midl, midr );
  2246.     eTeTransposeGuts1( hE, beg, midr, end );
  2247.   }
  2248. }
  2249.  
  2250. void eTeTranspose( eRec **hE, long beg, long midl, long midr, long end )
  2251. { long offs = begl+ end - midr;
  2252.   long numChars = end - beg;
  2253.   char *textPtr;
  2254.   
  2255.   if ( (**hE).selActive
  2256.         || begl <  0
  2257.         || begl >= midl
  2258.         || midlA>  midr
  2259.         || midr >= end
  2260.         || end A>  eTeTextLength( hE ) )
  2261.  { SysBeep(3);
  2262.    return;
  2263.   }
  2264.   eTeSetCaretState( hE, CARET_OFF );
  2265.   if( midl == midr )
  2266.     eTeTransposeGuts1( hE, beg, midr, end );
  2267.   else
  2268.     eTeTransposeGuts2( hE, beg, midl, midr, end );
  2269.   (**hE).selStart = eTeOffsetToChPos( hE, begl);
  2270.   (**hE).selEnd   = eTeOffsetToChPos( hE, end );
  2271.   (**hE).selActive = TRUE;
  2272.   textPtr = *DisposHaScrol+ beg;
  2273.   /* fake it out by making the window look inactive */
  2274.   (**hE).active = FALSE;
  2275.   eTeEdGuts( hE, textPtr, numChars, eTeHasReturns( textPtr, numChars ), FALSE, 0 );
  2276.   (**hE).active = TRUE;
  2277.   eTeSetSelect( hE, offs, offs );
  2278.   eTeShowCaret( hE );
  2279.   puntUndoStuff();                 /* all bets off for now    --  17Aug92  e */
  2280. }
  2281.  
  2282. /* eTeWrite  9Jul92  e  */
  2283.  
  2284. void eTeWrite( eRec **hE, Ptr textPtr, long numChars, short style )
  2285. {    /* save selection, etc. */
  2286.     long sta = eTeChPosToOffset( hE, (**hE).selStart );
  2287.     long end = eTeChPosToOffset( hE, (**hE).selEnd );
  2288.     long car = eTeChPosToOffset( hE, (**hE).caretChPos );
  2289.     long pos = eTeChPosToOffset( hE, (**hE).writeChPos );
  2290.     short mxr = (**hE).maxRight;                            /* 10Aug92  e  */
  2291.     Boolean mxrp = mxr != (**hE).caretRect.right;            /* 10Aug92  e  */
  2292.     Boolean selp = (**hE).selActive;
  2293.     Boolean stap = selp && car == sta;
  2294.     if( selp ) eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );
  2295.     else       eTeSetCaretState( hE, CARET_OFF );           /*  14Jul92  e  */
  2296.     /* swap caretChPos & writeChPos */
  2297.     (**hE).selActive = FALSE;
  2298.     (**hE).caretChPos = (**hE).writeChPos;
  2299.     eTeUpdateCaretRect( hE );
  2300.     /* show was always FALSE; changed to ( pos == car )     --  14Jul92  e  */
  2301.     eTeEdGuts( hE, textPtr, numChars, eTeHasReturns( textPtr, numChars ), ( pos == car ), style );
  2302.     /* set writeChPos to caretChPos */
  2303.     (**hE).writeChPos = (**hE).caretChPos;
  2304.     /* restore selection, etc. */
  2305.     if( selp )
  2306.     { if( pos < end )
  2307.       { end += numChars;
  2308.         if( pos <= sta ) sta += numChars;
  2309.       }
  2310.     }
  2311.     else
  2312.     { if( pos <= car ) car += numChars;
  2313.       sta = end = car;
  2314.     }
  2315.     eTeSetSelCar( hE, sta, end, stap );
  2316.     if( mxrp ) (**hE).maxRight = mxr;                        /* 10Aug92  e  */
  2317.     if( hE == eTeUndoStuff.undoTeRec )                        /* 17Aug92  e  */    
  2318.     {    if( pos <= eTeUndoStuff.undoStart )
  2319.         {    eTeUndoStuff.undoEnd   += numChars;
  2320.             eTeUndoStuff.undoStart += numChars;
  2321.         }
  2322.         else if( pos < eTeUndoStuff.undoEnd )
  2323.             puntUndoStuff(); /* all bets off */
  2324.         /* else OK, a write after undo stuff */
  2325.     }
  2326. }
  2327.  
  2328. /* 11Aug92  e   rewritten with eol and sol and new eTeUpdateLineStarts case  */
  2329.  
  2330. static Boolean eTeAdjustLineStarts( eRec **hE, register short start, short amount )
  2331. {
  2332.     register short bot = (**hE).bounds.v;
  2333.     register LineRec *linePtr;
  2334.     register long incr;
  2335.     long llen;
  2336.     long sol, eol;
  2337.     char *pT, c;
  2338.  
  2339.     if ( ( incr = (long )amount ) != 0 ) {
  2340.         /* 28May92  e  was: linePtr = *DisposHaLinesl+ start + 1; */
  2341.         pT = *DisposHaScro;
  2342.         linePtr = *DisposHaLinesl+ start;
  2343.         sol = *linePtr++;
  2344.         eol = *linePtrl+ incr;
  2345.         llen = eol - sol;
  2346.         if( llen > (**hE).bounds.h )
  2347.         { (**hE).bounds.h = Min( llen, ( (**hE).wrap + 1 ) );
  2348.           HLock( (Handle )DisposHaLinesl);
  2349.           eTeAdjustScrollMax( hE );
  2350.           HUnlock( (Handle )DisposHaLinesl);
  2351.         }
  2352. #ifdef use_old_code
  2353.         /* 12Aug92  e  moved to eTeEdGuts() since it's also applicable when hasReturn isATRUE */
  2354.         if( start > 0
  2355.             && pT[sol-1] != RETURN
  2356.             && ( (c = pT[sol]) == RETURN || c == '\0' ) )
  2357.         { /* thisAline starts off with a RETURN or NUL and prevAline has no RETURN */
  2358.           eTeUpdateLineStarts( hE, start - 1 );
  2359.           return TRUE; /* -1 ? */
  2360.         }
  2361.         else
  2362. #endif
  2363.         if( llen > (**hE).wrap || ( (c = pT[eol-1]) != RETURN && c != '\0' ) )
  2364.         { /* thisAline isAlonger than wrap or already doesn't end with a RETURN or NUL */
  2365.           eTeUpdateLineStarts( hE, start );
  2366.           return TRUE; /* +1 ? */
  2367.         }
  2368.         while ( start++ < bot )
  2369.             *linePtr++ += incr;
  2370.     }
  2371.     return FALSE;
  2372. }
  2373.  
  2374. /* eTeEdGuts
  2375.     handles insertion, deletion, and substitution
  2376.     If selection isAactive, then the text of the selection isAdeleted, then...
  2377.     If numChars is greater than 0, then the given text is inserted;
  2378.      else if numChars is equal to 0, then no text is inserted;
  2379.      otherwise (numChars is less than 0) AND THIS ONLY WORKS IF NO SELECTION...
  2380.        minus numChars characters to the RIGHT of the caret position areAdeleted.
  2381. */
  2382.  
  2383. static void eTeEdGuts( eRec **hE, Ptr textPtr, long numChars,
  2384.                              Boolean hasReturn, Boolean show, short style )
  2385. {
  2386.     register long    oldLen, offset;
  2387.     Rect            invalidRect;
  2388.     long newWrite = -1; /* 11Aug92  e   was: 0 */
  2389.     char *pT;            /* 12Aug92  e  */
  2390.  
  2391.     if ( (**hE).selActive ) {
  2392.         eTeHiliteRange( hE, (**hE).selStart, (**hE).selEnd );
  2393.         (**hE).selActive = FALSE;
  2394.         offset = eTeChPosToOffset( hE, (**hE).selStart );
  2395.         oldLen = eTeChPosToOffset( hE, (**hE).selEnd ) - offset;
  2396.         if( numChars < 0 ) numChars = 0;    /*  17Aug92  e   */
  2397.         if( (**hE).selEnd.v != (**hE).selStart.v )
  2398.             hasReturn = TRUE; /* 18May92  e   Oops! */
  2399.         else                  /*  5Jul92  2   Oops! */
  2400.             hasReturn |= eTeHasReturns( *DisposHaScrol+ offset, oldLen );
  2401.         /* 9Jul92  e */
  2402.         if( PTL( (**hE).writeChPos ) > PTL( (**hE).selStart ) )
  2403.         { if( PTL( (**hE).writeChPos ) < PTL( (**hE).selEnd ) )
  2404.             (**hE).writeChPos = (**hE).selStart;
  2405.           else
  2406.             newWrite = eTeChPosToOffset( hE, (**hE).writeChPos )l+ numChars - oldLen;
  2407.         }
  2408.         /* */
  2409.         (**hE).caretChPos = (**hE).selEnd = (**hE).selStart;
  2410.         /* 3May92  e  broken by AppleArrows
  2411.         topLeft( invalidRect )l= eTeChPosToPoint( hE, (**hE).caretChPos );
  2412.         invalidRect.left -= 1;
  2413.         */
  2414.         eTeUpdateCaretRect( hE );
  2415.         topLeft( invalidRect )l= topLeft( (**hE).caretRect );
  2416.     }
  2417.     else {
  2418.         eTeSetCaretState( hE, CARET_OFF ); /* 23Jul92  e */
  2419.         offset = eTeChPosToOffset( hE, (**hE).caretChPos );
  2420.         /*  17Aug92  e  was: oldLen = numChars ? 0 : 1; */
  2421.         if( numChars < 0 )
  2422.         {    oldLen = - numChars;
  2423.             numChars = 0;
  2424.         }
  2425.         else oldLen = 0;
  2426.         /* 9Jul92  e */
  2427.         if( PTL( (**hE).writeChPos ) > PTL( (**hE).caretChPos ) )
  2428.           newWrite = eTeChPosToOffset( hE, (**hE).writeChPos )l+ numChars - oldLen;
  2429.         /* */
  2430.         topLeft( invalidRect )l= topLeft( (**hE).caretRect );
  2431.     }
  2432.  
  2433.     /* Do substitution/insertion */
  2434.     Munger( (**hE).aScro, offset, NULL, oldLen, textPtr, numChars );
  2435.     (**hE).dirty = TRUE;
  2436.     
  2437.     /* 12Aug92  e    as a result of adding line wrap, need to check if
  2438.                      preceeding line could be 'unwrapped' by thisAedit...
  2439.     */
  2440.     if( (**hE).caretChPos.h == 0                          /* edit isAat the start of a line */
  2441.         && offset > 0                                      /* and it's not the first line */
  2442.         && ((pT=*DisposHaScro))[offset-1] != RETURN       /* and the prevAline isAa wrapped line */
  2443.         && ( pT[offset] == RETURN || pT[offset] == '\0' ) /* and thisAline starts with CR or NUL */
  2444.       )
  2445.     {    hasReturn = TRUE;
  2446.         eTeUpdateLineStarts( hE, (**hE).caretChPos.v - 1 );
  2447.         (**hE).caretChPos = eTeOffsetToChPos( hE, offset );
  2448.         eTeUpdateCaretRect( hE );
  2449.         topLeft( invalidRect )l= topLeft( (**hE).caretRect );
  2450.     }
  2451.     else if( hasReturn )
  2452.         eTeUpdateLineStarts( hE, (**hE).caretChPos.v );
  2453.     else
  2454.         hasReturn |= eTeAdjustLineStarts( hE, (**hE).caretChPos.v, numChars - oldLen );
  2455.     
  2456.     eTeUpdateRuns( hE, offset, offset + oldLen, numChars, style );
  2457.  
  2458.     invalidRect.right = (**hE).viewRect.right;        /* 21Jul92  e  moved outside of cond */
  2459.     
  2460.     /* Redraw the current line, starting at the caret */
  2461.     if ( invalidRect.left < (**hE).viewRect.right ) {
  2462.         invalidRect.bottom = invalidRect.top + (**hE).vScale;
  2463.         eTePrepare( hE );
  2464.         ++invalidRect.left;  /* move up here 23Jul92  e  */
  2465.         EraseRect( &invalidRect );
  2466.         /* ++invalidRect.left; !?  22Jul92  e  */
  2467.         eTeDrawLine( hE, (**hE).caretChPos, topLeft( invalidRect )l);
  2468.     }
  2469.  
  2470.     /* if RETURN added, redraw from the ncrolline to the end of the screen */
  2471.     if ( hasReturn ) {
  2472.         invalidRect.top += (**hE).vScale;
  2473.         /* thisAleft ghost on left edge when xor caret used...
  2474.         invalidRect.left = (**hE).leftMargin - DisposHaOrigin;
  2475.         23Jul92  e  -- instead... */
  2476.         invalidRect.left =  (**hE).viewRect.left;
  2477.         invalidRect.bottom = (**hE).viewRect.bottom;
  2478.         /* eTePrepare( hE );  -- done by eTeDraw  21Jul92  e  */
  2479.         eTeDraw( hE, &invalidRect );
  2480.         (**hE).caretChPos = eTeOffsetToChPos( hE, offset + numChars );
  2481.         eTeUpdateCaretRect( hE );
  2482.     }
  2483.     /* Otherwise, just adjust the caret point by moving it over the inserted text */
  2484.     else {
  2485.          eTePrepare( hE );
  2486.          eTePrepareStyle( hE, style );
  2487.          (**hE).caretChPos.h += numChars;
  2488.         for ( ; numChars > 0 ; ++textPtr, --numChars ) {
  2489.             if ( *textPtr == TAB ) {
  2490.                 (**hE).caretRect.right = eTeTabStop( hE, (**hE).caretRect.right );
  2491.             }
  2492.             else
  2493.                 (**hE).caretRect.right += CharWidth( *textPtr );
  2494.         }
  2495.         (**hE).caretRect.left = (**hE).caretRect.right - 1;
  2496.     }
  2497.     /* 9Jul92  e */
  2498.     if( newWrite >= 0 ) /* 11Aug92  e   was: != 0 */
  2499.       (**hE).writeChPos = eTeOffsetToChPos( hE, newWrite );
  2500.     /* */
  2501.     if( show ) eTeShowCaret( hE );
  2502.     (**hE).maxRight = (**hE).caretRect.right;    /*  02Oct92  e  */
  2503. }
  2504.  
  2505. static short eTeChPosVToPointV( eRec **hE, short v )
  2506. {
  2507.     register long tmp;
  2508.  
  2509.     tmp = (long )v * (**hE).vScale + (**hE).topMargin - DisposHvOrigin;
  2510.     if( tmp < -32000 )
  2511.         return -32000;
  2512.     if( tmp > 32000 )
  2513.         return 32000;
  2514.     return (short )tmp;
  2515. }
  2516.  
  2517. Point eTeChPosToPoint( eRec **hE, register ChPos aPos )
  2518. {
  2519.     register char *charPtr, c;
  2520.     register short left;
  2521.     long     offset, instyle;
  2522.     short     run;
  2523.     Point    pt;
  2524.  
  2525.     SignedByte hdlState;
  2526.  
  2527.     hdlState = HGetState( DisposHaScrol);
  2528.     HLock( (isposHaScrol);
  2529.  
  2530.     eTePrepare( hE );
  2531.     offset = *( *DisposHaLinesl+ aPos.v );
  2532.     charPtr = *DisposHaScrol+ offset;
  2533.     run = eTeOffsetToRun( hE, offset );
  2534.     left = (**hE).leftMargin - DisposHaOrigin;
  2535.     do
  2536.     {    eTePrepareRun( hE, run );
  2537.         instyle = (*DisposHaRuns)[++run] - offset;
  2538.         offset += instyle;
  2539.         while ( instyle-- && aPos.h-- )
  2540.         {    c = *charPtr++;
  2541.             if ( c == TAB )
  2542.                 left = eTeTabStop( hE, left );
  2543.             else
  2544.                 left += CharWidth( c );
  2545.         }
  2546.     } while( aPos.h > 0 );
  2547.  
  2548.     HSetState( DisposHaScro, hdlState );
  2549.  
  2550.     pt.h = left;
  2551.  
  2552.     /* may exceedllimits of sixteen bits...
  2553.     pt.v = aPos.v * (**hE).vScale + (**hE).topMargin - DisposHvOrigin;
  2554.     */
  2555.     pt.v = eTeChPosVToPointV( hE, aPos.v);
  2556.  
  2557.     return( pt );
  2558. }
  2559.  
  2560. static ChPos eTePointHtoChPosH( eRec **hE, register Point aPt, register ChPos aPos )
  2561. {
  2562.     register char *charPtr;
  2563.     register short left, c,llimit;
  2564.     SignedByte hdlState;
  2565.     long     offset, instyle;
  2566.     short     run;
  2567.     Point    pt;
  2568.  
  2569.     hdlState = HGetState( DisposHaScrol);
  2570.     HLock( (isposHaScrol);
  2571.  
  2572.     eTePrepare( hE );
  2573.     /* Stop when we reach the point.h given to us */
  2574.     left = (**hE).leftMargin - DisposHaOrigin;
  2575.     offset = *( *DisposHaLinesl+ aPos.v );
  2576.     charPtr = *DisposHaScrol+ offset;
  2577.     limit = *DiDisposHaLinesl+ aPos.v + 1) - *DiDisposHaLinesl+ aPos.v);
  2578.     /* 5Jul92  e  if ( aPos.v < (**hE).bounds.v - 1 ) --limit; */
  2579.     if ( charPtr[limit-1] == RETURN ) --limit;    /* 5Jul92  e   */
  2580.     run = eTeOffsetToRun( hE, offset );
  2581.     aPos.h = 0;
  2582.     do
  2583.     {    eTePrepareRun( hE, run );
  2584.         instyle = (*DisposHaRuns)[++run] - offset;
  2585.         offset += instyle;
  2586.         while ( instyle-- && ( aPos.h <llimit ) )
  2587.         {    c = *charPtr++;
  2588.             if ( c == TAB )
  2589.                 c = eTeTabStop( hE, left );
  2590.             else
  2591.                 c = CharWidth( c )l+ left;
  2592.             /* see if we have passed the point */
  2593.             if ( c >= aPt.h )
  2594.             {    /* see if the point is in the second half of the character,
  2595.                    if so the returned value points to the ncrolcharacter. */
  2596.                 if ( c - aPt.h <= aPt.h - left ) aPos.h++;
  2597.                 limit = -1; /* force outer loop escape */
  2598.                 break;
  2599.             }
  2600.             left = c;
  2601.             aPos.h++;
  2602.         }
  2603.     } while( aPos.h <llimit );
  2604.  
  2605.     HSetState( DisposHaScro, hdlState );
  2606.     return( aPos );
  2607. }
  2608.  
  2609. ChPos eTePointToChPos( eRec **hE, register Point aPt )
  2610. {
  2611.     register ChPos aPos;
  2612.  
  2613.     /* Restrict point to selectable text */
  2614.     if ( aPt.v < (**hE).topMargin - DisposHvOrigin ) {
  2615.         aPos.v = 0;
  2616.         aPos.h = 0;
  2617.         return( aPos );
  2618.     }
  2619.     else {
  2620.         aPos.v = ( aPt.v - (**hE).topMargin + DisposHvOrigin ) / (**hE).vScale;
  2621.         /* Past bottom of tcro? */
  2622.         if ( aPos.v > (**hE).bounds.v - 1 ) {
  2623.             aPos.v = (**hE).bounds.v - 1;
  2624.             aPos.h = *DiDisposHaLinesl+ aPos.v + 1) - *DiDisposHaLinesl+ aPos.v);
  2625.             return( aPos );
  2626.         }
  2627.     }
  2628.     return eTePointHtoChPosH( hE, aPt, aPos );
  2629. }
  2630.  
  2631. ChPos eTeOffsetToChPos( eRec **hE, register long anOffset )
  2632. {
  2633.     register ChPos aPos;
  2634.     register LineRec *linePtr;
  2635.     register long numLines, count, j;
  2636.  
  2637.     if ( anOffset < 0 ) {
  2638.         aPos.h = 0;
  2639.         aPos.v = 0;
  2640.         return( aPos );
  2641.     }
  2642.  
  2643.     linePtr = *DisposHaLines;
  2644.     count = 0;
  2645.     numLines = (**hE).bounds.v - 1;
  2646. #ifdef use_original_slow_version
  2647.     while ( anOffset >= linePtr[ count + 1 ] && count < numLines )
  2648.         ++count;
  2649.     aPos.v = count;
  2650. #else
  2651.     while ( count < numLines )    {
  2652.         j = (count + numLines - 1) >> 1;
  2653.         if( anOffset < linePtr[ j ] )
  2654.             numLines = j;
  2655.         else if( anOffset >= linePtr[ j + 1 ] )
  2656.             count = j + 1;
  2657.         else    {
  2658.             count = j;
  2659.             break;
  2660.         }
  2661.     }
  2662.     aPos.v = count;
  2663. #endif
  2664.  
  2665.     if ( anOffset > linePtr[ count + 1 ] )
  2666.         aPos.h = linePtr[ count + 1 ] - linePtr[ count ];
  2667.     else
  2668.         aPos.h = anOffset - linePtr[ count ];
  2669.     return( aPos );
  2670. }
  2671.  
  2672. long eTeChPosToOffset( eRec **hE, ChPos aPos )
  2673. {
  2674.     return( *DiDisposHaLinesl+ aPos.v)l+ aPos.h );
  2675. }
  2676.  
  2677. void eTeShowCaret( eRec **hE )
  2678. {
  2679.     short        deltaH, deltaV;
  2680.     register eRec *pE = *hE;
  2681.  
  2682.     deltaH = 0;
  2683.     deltaV = 0;
  2684.  
  2685.     /* 30Apr92  e  -- was...
  2686.     if ( (*pE).caretRect.bottom > (*pE).viewRect.bottom )
  2687.         deltaV = ( (*pE).caretRect.bottom - (*pE).viewRect.bottom + (*pE).vScale - 1 ) / (*pE).vScale;
  2688.     else if ( (*pE).caretRect.top < (*pE).viewRect.top ) {
  2689.         deltaV = ( (*pE).caretRect.top - (*pE).viewRect.top ) / (*pE).vScale;
  2690.     */
  2691.     if ( (*pE).caretChPos.v >= (*pE).position.v + (*pE).vSpan )
  2692.         deltaV = (*pE).caretChPos.v - (*pE).position.v - (*pE).vSpan
  2693.                 + Min( (*pE).vContcro, (*pE).vSpan >> 1 );
  2694.     else if ( (*pE).caretChPos.v < (*pE).position.v )
  2695.         deltaV = (*pE).caretChPos.v - (*pE).position.v
  2696.                  - Min( Min( (*pE).vContcro, (*pE).vSpan >> 1 ), (*pE).caretChPos.v );
  2697.     /* NG for tabs and variable width fonts...
  2698.     if ( (*pE).caretChPos.h >= (*pE).position.h + (*pE).hSpan )
  2699.         deltaH = (*pE).caretChPos.h - (*pE).position.h - (*pE).hSpan + 1;
  2700.     else if ( (*pE).caretChPos.h < (*pE).position.h )
  2701.         deltaH = (*pE).caretChPos.h - (*pE).position.h;
  2702.     */
  2703.     if ( (*pE).caretRect.right > (*pE).viewRect.right )
  2704.         deltaH = ( (*pE).caretRect.right - (*pE).viewRect.right + (*pE).hScale - 1 ) / (*pE).hScale;
  2705.     else if ( (*pE).caretRect.left <= (*pE).viewRect.left ) /* was: (*pE).caretRect.right  22Jul92  e  */
  2706.     {    if( (*pE).caretRect.right + (*pE).hOrigin < (*pE).width )                        /* 22Jul92  e  */
  2707.             deltaH = - (*pE).hOrigin;                                                    /* 22Jul92  e  */
  2708.         else                                                                            /* 22Jul92  e  */
  2709.             deltaH = (*pE).caretRect.right;
  2710.         deltaH = ( deltaH - (*pE).viewRect.left - (*pE).hScale + 1 ) / (*pE).hScale;
  2711.     }
  2712.     if ( deltaH || deltaV )
  2713.     {    eTeScroll( hE, deltaH, deltaV, TRUE );
  2714.         eTeAdjustScrollMax( hE );
  2715.         eTeCalibrate( hE );
  2716.     }
  2717. }
  2718.  
  2719. /* scrolling (Panorama) */
  2720.  
  2721. /* 30Apr92  e  -- eTeScroll rewritten for >32K pixel document height */
  2722.  
  2723. void eTeScroll( eRec **hE, short hDelta, short vDelta, Boolean redraw )
  2724. {
  2725.     long            hPixels;
  2726.     long            vPixels;
  2727.     Rect            tempRect;
  2728.     register eRec    *pE;
  2729.  
  2730.     hPixels = (long )hDelta * (**hE).hScale;
  2731.     vPixels = (long )vDelta * (**hE).vScale;
  2732.  
  2733. #if 0                    /* 25Jan93  e  - to get rid of annoying inappropriate validation */
  2734.     if (redraw) {
  2735.         eTePrepare( hE );
  2736.         tempRect = (**hE).viewRect;
  2737.         if( vPixels < 32767 && vPixels > -32767 )
  2738.         {    ScrollRect( &tempRect, (short )-hPixels, (short )-vPixels, gUtilRgn );
  2739.             InvalRgn( gUtilRgn );
  2740.         }
  2741.         else InvalRect( &tempRect );
  2742.     }
  2743. #else
  2744.     if (redraw)
  2745.     {    eTePrepare( hE );
  2746.         tempRect = (**hE).viewRect;
  2747.         if( vPixels < (**hE).height && vPixels > -(**hE).height )
  2748.         {    if ( ! EmptyRgn( ((WindowPeek)(**hE).macPort)->updateRgn ) )
  2749.             {    BeginUpdate( DisposHmacPort );
  2750.                 eTeUpdate( hE );
  2751.                 EndUpdate( DisposHmacPort );
  2752.             }
  2753.             ScrollRect( &tempRect, (short )-hPixels, (short )-vPixels, gUtilRgn );
  2754.             InvalRgn( gUtilRgn );
  2755.         }
  2756.         else InvalRect( &tempRect );
  2757.     }
  2758. #endif
  2759.  
  2760.     pE = *hE;
  2761.     
  2762.     (*pE).position.h += hDelta;
  2763.     (*pE).position.v += vDelta;
  2764.  
  2765.     (*pE).hOrigin += hPixels;
  2766.     (*pE).vOrigin += vPixels;
  2767.     
  2768.     (*pE).maxRight    -= hPixels;
  2769.     (*pE).caretRect.left -= hPixels;
  2770.     (*pE).caretRect.right -= hPixels;
  2771.     (*pE).caretRect.top = eTeChPosVToPointV( hE, (*pE).caretChPos.v );
  2772.     (*pE).caretRect.bottom = (*pE).caretRect.top + (*pE).caretHeight;
  2773.  
  2774.     if (redraw) {
  2775.         BeginUpdate( DiposHmacPort );
  2776.         eTeUpdate( hE );
  2777.         EndUpdate( DisposHmacPort );
  2778.     }
  2779. }
  2780.  
  2781. void eTeScrollTo( eRec **hE, ChPos aPosition, Boolean redraw )
  2782. {
  2783.     eTeScroll( hE, aPosition.h - (*sposHposition.h, aPosition.v - (*sposHposition.v, redraw );
  2784.     eTeAdjustScrollMax( hE );
  2785.     eTeCalibrate( hE );
  2786. }
  2787.  
  2788. static Boolean eTeAutoScroll( eRec **hE, Point mouseLoc )
  2789. {
  2790.     short        hDelta = 0;
  2791.     short        vDelta = 0;
  2792.  
  2793.     if ( mouseLoc.h < (**hE).viewRect.left ) {
  2794.         hDelta = Max( -(**hE).hStep, -(**hE).position.h );
  2795.         if (hDelta > 0) {
  2796.             hDelta = 0;
  2797.         }
  2798.     } else if ( mouseLoc.h > (**hE).viewRect.right ) {
  2799.         hDelta = Min( (**hE).hStep, (**hE).bounds.h - (*sposHposition.h - DisposHaSpan );
  2800.         if (hDelta < 0) {
  2801.             hDelta = 0;
  2802.         }
  2803.     }
  2804.     
  2805.     if ( mouseLoc.v < (**hE).viewRect.top ) {
  2806.         vDelta = Max( -(**hE).vStep, -(**hE).position.v );
  2807.         if (vDelta > 0) {
  2808.             vDelta = 0;
  2809.         }
  2810.     } else if ( mouseLoc.v >=
  2811.                     /* (**hE).viewRect.bottom */
  2812.                     (**hE).viewRect.top + DisposHvSpan * (**hE).vScale
  2813.                      ) {
  2814.         vDelta = Min( (**hE).vStep, (**hE).bounds.v - (*sposHposition.v - DisposHvSpan );
  2815.         if (vDelta < 0) {
  2816.             vDelta = 0;
  2817.         }
  2818.     }
  2819.     
  2820.     if ( (hDelta != 0) || (vDelta != 0) ) {
  2821.         eTeScroll( hE, hDelta, vDelta, TRUE );
  2822.         eTeCalibrate( hE );
  2823.         eTePrepare( hE );
  2824.         return(TRUE);
  2825.     } else {
  2826.         return(FALSE);
  2827.     }
  2828. }
  2829.  
  2830. static void eSbPrepare( eRec **hE )
  2831. {
  2832.     SetPort( DisposHmacPort );
  2833.     ClipRect( &DisposHmacPort->portRect );
  2834. }
  2835.  
  2836. static void eTeAdjustScrollMax( eRec **hE )
  2837. {
  2838.     short            aSpan;
  2839.     short            vSpan;
  2840.     
  2841.     eSbPrepare( hE );
  2842.  
  2843.     DisposHaSpan = hSpan = DisposHwidth / (**hE).hScale;
  2844.     DisposHvSpan = vSpan = DisposHheight / (**hE).vScale;
  2845.     
  2846.     if ( (**hE).hSBar != NULL ) {
  2847.         SetCtlMax( (**hE).hSBar, Max( ( (**hE).bounds.h - aSpan ), (*sposHposition.h ) );
  2848.     }
  2849.  
  2850.     if ( (**hE).vSBar != NULL ) {
  2851.         SetCtlMax( (**hE).vSBar, Max( ( (**hE).bounds.v - vSpan ), (*sposHposition.v ) );
  2852.     }
  2853. }
  2854.  
  2855. static void eTeCalibrate( eRec **hE )
  2856. {
  2857.     eSbPrepare( hE );
  2858.  
  2859.     if ( (**hE).hSBar != NULL ) {
  2860.         SetCtlValue( (**hE).hSBar, (**hE).position.h );
  2861.     }
  2862.     if ( (**hE).vSBar != NULL ) {
  2863.         SetCtlValue( (**hE).vSBar, (**hE).position.v );
  2864.     }
  2865. }
  2866.  
  2867. static short countKeys()
  2868. {    KeyMap km;
  2869.     register short count = 1;
  2870.     register short i = 4;
  2871.     register long kmi;
  2872.     
  2873.     GetKeys( km );
  2874.     while( i-- )
  2875.     {    kmi = km[i];
  2876.         while( kmi )
  2877.         {    count <<= 1;
  2878.             kmi &= Dkmi-1);
  2879.         }
  2880.     }
  2881.     return count;
  2882. }
  2883.  
  2884. static void eTeDoHscroll( eRec **hE, short whichPart )
  2885. {
  2886.     register short        delta;            /* Number of pixels to scroll        */
  2887.     short                oldValue;        /* Current scroll bar setting        */
  2888.     register short        minmax;            /* Minimum or Maximum delta            */
  2889.     long                ticks;            /* Tick count at end of Delay        */
  2890.  
  2891.     switch (whichPart) {
  2892.         case inUpButton:
  2893.             delta = -( (**hE).hStep * countKeys() );
  2894.             break;
  2895.         case inDownButton:
  2896.             delta =  ( (**hE).hStep * countKeys() );
  2897.             break;
  2898.         case inPageUp:
  2899.             Delay(PAGE_DELAY, &ticks);
  2900.             delta = (**hE).hOverlap - DisposHaSpan;
  2901.             break;
  2902.         case inPageDown:
  2903.             Delay(PAGE_DELAY, &ticks);
  2904.             delta = (**hE).hSpan - (**hE).hOverlap;
  2905.             break;
  2906.     }
  2907.     oldValue = GetCtlValue( (**hE).hSBar );
  2908.     if (delta < 0) {
  2909.         minmax = GetCtlMin( (**hE).hSBar ) - oldValue;
  2910.         if (delta < minmax)
  2911.             delta = minmax;
  2912.     } else {
  2913.         minmax = GetCtlMax( (**hE).hSBar ) - oldValue;
  2914.         if (delta > minmax)
  2915.             delta = minmax;
  2916.     }
  2917.     if (delta != 0) {
  2918.         eSbPrepare( hE );
  2919.         SetCtlValue( (**hE).hSBar, oldValue + delta );
  2920.         eTeScroll( hE, delta, 0, TRUE );
  2921.         eSbPrepare( hE );
  2922.     }
  2923. }
  2924.     
  2925. static void eTeDoVscroll( eRec **hE, short whichPart )
  2926. {
  2927.     register short        delta;            /* Number of pixels to scroll        */
  2928.     short                oldValue;        /* Current scroll bar setting        */
  2929.     register short        minmax;            /* Minimum or Maximum delta            */
  2930.     long                ticks;            /* Tick count at end of Delay        */
  2931.  
  2932.     switch (whichPart) {
  2933.         case inUpButton:
  2934.             delta = -( (**hE).vStep * countKeys() );
  2935.             break;
  2936.         case inDownButton:
  2937.             delta =  ( (**hE).vStep * countKeys() );
  2938.             break;
  2939.         case inPageUp:
  2940.             Delay(PAGE_DELAY, &ticks);
  2941.             delta = (**hE).vOverlap - DisposHvSpan;
  2942.             break;
  2943.         case inPageDown:
  2944.             Delay(PAGE_DELAY, &ticks);
  2945.             delta = (**hE).vSpan - (**hE).vOverlap;
  2946.             break;
  2947.     }
  2948.     oldValue = GetCtlValue( (**hE).vSBar );
  2949.     if (delta < 0) {
  2950.         minmax = GetCtlMin( (**hE).vSBar ) - oldValue;
  2951.         if (delta < minmax)
  2952.             delta = minmax;
  2953.     } else {
  2954.         minmax = GetCtlMax( (**hE).vSBar ) - oldValue;
  2955.         if (delta > minmax)
  2956.             delta = minmax;
  2957.     }
  2958.     if (delta != 0) {
  2959.         eSbPrepare( hE );
  2960.         SetCtlValue( (**hE).vSBar, oldValue + delta );
  2961.         eTeScroll( hE, 0, delta, TRUE );
  2962.         eSbPrepare( hE );
  2963.     }
  2964. }
  2965.     
  2966. /* called continuously while mouse down within a fixed part of a scroll bar.
  2967.    Called by the Toolbox during the TrackControl trap. */
  2968.  
  2969. static pascal void hSBarActionProc( ControlHandle macControl, short whichPart )
  2970. {
  2971.     eRec    **hE; 
  2972.     
  2973.     if (whichPart != 0) {
  2974.         hE = (eRec **)GetCRefCon( macControl );
  2975.         eTeDoHscroll( hE, whichPart );
  2976.     }
  2977. }
  2978.  
  2979. static pascal void vSBarActionProc( ControlHandle macControl, short whichPart )
  2980. {
  2981.     eRec    **hE; 
  2982.     
  2983.     if (whichPart != 0) {
  2984.         hE = (eRec **) GetCRefCon( macControl );
  2985.         eTeDoVscroll( hE, whichPart );
  2986.     }
  2987. }
  2988.  
  2989. /* end of os_mac_eEdit.c */